Выравнивание текста UILabel, когда английский и иврит смешиваются как текст?

У меня есть UILabel с текстом на английском и иврите. Я хотел выровнять в соответствии с языковыми средствами для английского LTL и для иврита RTL.

Текст

Благословляю Тебя, Боже, за сотворение плода виноградной лозы:

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


person Sanoj    schedule 27.06.2014    source источник
comment
Попробуйте с NSAttributedString, установив NSParagraphStyle, а затем [yourLabel setAttributedText:theAttributedString]   -  person Larme    schedule 27.06.2014
comment
NSAttributedString не может установить свойство Alignment. любой рабочий код. Это бы мне очень помогло.   -  person Sanoj    schedule 27.06.2014
comment
У вас есть хотя бы образец текста? Выравнивание задается в файле NSParagraphStyle.   -  person Larme    schedule 27.06.2014


Ответы (1)


Вот пример, который может вас вдохновить.

Примечание. Я не знаю, как вы создаете весь свой текст, поэтому я не знаю, как вы узнаете, что на английском, а что на иврите, но вы уловите идею.

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])];
person Larme    schedule 27.06.2014
comment
Отличное решение, хотя вы можете создать 2 ярлыка, каждый из которых будет содержать разный язык. - person OhadM; 30.12.2015