UILabel text alignment when English and Hebrew mixed as text?

非 Y 不嫁゛ 提交于 2019-12-08 03:16:49

问题


I have UILabel which having a text which is mixed of English and Hebrew. I wanted to alignment as per language means for English LTL and for Hebrew RTL.

Text is

I bless You, God, for creating the fruit of the vine:

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


回答1:


Here is a sample you could be inspired with.

Note: I don't know how you create your all text, so I don't know how you'll know what's in English or what in Hebrew, but you'll get the idea.

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];

You also may want to do this before setting it to your UILabel:

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


来源:https://stackoverflow.com/questions/24456260/uilabel-text-alignment-when-english-and-hebrew-mixed-as-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!