AutoLayout + RTL + UILabel 文本对齐 [英] AutoLayout + RTL + UILabel text alignment

查看:23
本文介绍了AutoLayout + RTL + UILabel 文本对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于开始与自动布局搏斗,但似乎无法弄清楚如何获得从右到左 (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.这会从加载的应用程序语言(not 来自脚本)推断文本对齐方式.

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屋!

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