2014-06-27 37 views
1

我有UILabel其中有一個混合了英文和希伯來文的文本。我想根據英語LTL和希伯來語RTL的語言方式進行對齊。英語和希伯來語混合爲文本時UILabel文本對齊嗎?

文字

我祝福你,上帝,用於創建葡萄汁:

בָּרוּךְאַתָּהיְיָאֱלֹהֵֽינוּמֶֽלֶךְהָעוֹלָם,בּוֹרֵאפְּרִי הַגָּֽפֶן。

+0

嘗試用'NSAttributedString',創下了'NSParagraphStyle'然後'[yourLabel setAttributedText:theAttributedString]' – Larme

+0

NSAttributedString無法設置對齊屬性。任何工作代碼。這對我很有幫助。 – Sandy

+0

你至少有一個文本樣本?對齊在'NSParagraphStyle'中設置。 – Larme

回答

3

以下是您可以從中受到啓發的示例。

注意:我不知道如何創建所有文本,所以我不知道如何知道英文或希伯來文是什麼,但您會明白。

NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"I bless You, God, for creating the fruit of the vine:\n בָּרוּךְ אַתָּה יְיָ אֱלֹהֵֽינוּ מֶֽלֶךְ הָעוֹלָם, בּוֹרֵא פְּרִי הַגָּֽפֶן."]; 

NSMutableParagraphStyle *englishParagraphStyle = [[NSMutableParagraphStyle alloc] init]; 
[englishParagraphStyle setAlignment:NSTextAlignmentLeft]; 

NSMutableParagraphStyle *hebrewParagraphStyle = [[NSMutableParagraphStyle alloc] init]; 
[hebrewParagraphStyle setAlignment:NSTextAlignmentRight]; 

NSRange englishRange = NSMakeRange(0, [@"I bless You, God, for creating the fruit of the vine:" length]); 
NSRange hebrewRange = NSMakeRange([@"I bless You, God, for creating the fruit of the vine:" length], 
            [[attrStr string] length] - [@"I bless You, God, for creating the fruit of the vine:" length]); 

[attrStr addAttribute:NSParagraphStyleAttributeName 
       value:englishParagraphStyle 
       range:englishRange]; 
[attrStr addAttribute:NSParagraphStyleAttributeName 
       value:hebrewParagraphStyle 
       range:hebrewRange]; 

[myLabel setAttributedText:attrStr]; 

你也可以將其設置爲你的UILabel之前做到這一點:

[attrStr addAttribute:NSFontAttributeName 
       value:[UIFont whateverFontWithWhateverSize] 
       range:NSMakeRange(0, [attrStr length])]; 
+0

偉大的解決方案,儘管你可以創建2個標籤,他們每個人都將包含不同的語言。 – OhadM

相關問題