UILabel将英语和希伯来语混合为文本时的文本对齐方式? [英] UILabel text alignment when English and Hebrew mixed as text?

查看:123
本文介绍了UILabel将英语和希伯来语混合为文本时的文本对齐方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有UILabel,其文本混合了英语和希伯来语.我想按照英语LTL和希伯来语RTL的语言含义进行对齐.

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.

文字是

上帝保佑你们创造出葡萄树的果实:

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

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

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

推荐答案

以下是您可能会受到启发的示例.

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

您可能还需要在将其设置为UILabel之前进行此操作:

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

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

这篇关于UILabel将英语和希伯来语混合为文本时的文本对齐方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆