AutoLayout + RTL + UILabel 文字对齐方式 [英] AutoLayout + RTL + UILabel text alignment
问题描述
我终于开始尝试使用自动布局,但似乎无法弄清楚如何获得从右到左 (RTL) 的支持以按照我期望/想要的方式工作...
I'm finally getting round to wrestling with Auto Layout and can't seem to figure out how to get right-to-left (RTL) support to work the way I'd expect/want...
我在 Interface Builder 中设计了如下所示的视图:
I have designed the view in Interface Builder as shown:
使用英语时生成的应用程序按预期运行:
With the resulting app running as expected when using English:
然而,当切换到 RTL 语言(在这种情况下是阿拉伯语)时,整个视图翻转(这很棒)但 UILabel
的文本仍然左对齐.我希望它正确对齐以使其与 UIImageView
保持一致.
However when switching to an RTL language (Arabic in this case), the entire view flips (which is great) but the UILabel
's text is still left aligned. I'd expect it to be right aligned to keep it up against the UIImageView
.
显然我遗漏了一些东西和/或自动布局没有涵盖这一点.
Clearly I'm missing something and/or this isn't covered by Auto Layout.
我应该在使用 RTL 语言时手动设置 textAlignment
吗?
Am I supposed to set the textAlignment
manually when using an RTL language?
推荐答案
你想要 NSTextAlignmentNatural
.这从加载的应用程序语言(不是从脚本中)推断出文本对齐方式.
You want NSTextAlignmentNatural
. That infers the text alignment from the loaded application language (not from the script).
对于 iOS 9 及更高版本(使用 Xcode 7),您可以在故事板中进行设置(选择 --- 对齐选项).如果您需要针对早期版本,则需要为标签创建一个出口并在 awakeFromNib
中设置对齐方式.
For iOS 9 and later (using Xcode 7), you can set this in the storyboard (choose the --- alignment option). If you need to target earlier releases, you'll need to create an outlet to the label and set the alignment in awakeFromNib
.
- (void)awakeFromNib {
[[self label] setTextAlignment:NSTextAlignmentNatural];
}
这篇关于AutoLayout + RTL + UILabel 文字对齐方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!