1

使用适用于 iPad 的 iOS 5.0+,我有一个在 UITextView 中包含阿拉伯文本的应用程序,其中:

myTextView.editable = NO;

奇怪的是,阿拉伯文字突然左对齐而不是右对齐,给用户的可读性造成了很大的问题。

我感谢任何帮助克服这种奇怪的行为!

4

1 回答 1

6

解决了!事实证明这很简单:只需将 textAlignment 设置为 UITextAlignmentRight。

UITextView 在可编辑和不可编辑时的工作方式不同,尤其是在 RTL 文本方面。如果在可编辑的文本视图中基本书写方向是 RTL,则必须将文本左对齐,而不是右对齐,以便文本真正右对齐(RTL 书写方向翻转默认值!)

所以,当你有一个 UITextView 时,你可能要先检查可编辑属性,然后使用第一个字符的书写方向(这是 iOS 确定文本是左对齐还是右对齐)来设置 textAlignment 属性。

例如:

// check if the text view is both not editable and has an RTL writing direction
if (!someTextView.editable && [someTextView baseWritingDirectionForPosition:[someTextView beginningOfDocument] inDirection:UITextStorageDirectionForward] == UITextWritingDirectionRightToLeft) {
        // if yes, set text alignment right
        someTextView.textAlignment = UITextAlignmentRight;
    } else {
        // for all other cases, set text alignment left
        someTextView.textAlignment = UITextAlignmentLeft;
    }
}

iOS 6 的更新:

在 iOS 6 中,UITextView 的 textAlignment 属性实际上对应于它在屏幕上的外观。对于 iOS 6,只需将 textAlignment 设置为您想要查看的方向即可。上面的代码按照 iOS 5.1 和更早版本的描述工作。

我希望这可以帮助其他人处理这个问题!

于 2012-06-02T15:42:51.863 回答