UITextView对齐“从右到左”的书面语言 [英] UITextView alignment for 'right to left' written language

查看:222
本文介绍了UITextView对齐“从右到左”的书面语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两种方法来更改UITextView的文本对齐方式。我的目标是iOS6及以上版本。这个想法是用户可以改变textview输入的对齐方式。如果输入是英文,下面的方法工作正常。如果输入阿拉伯语,它不会改变对齐。

I have created two methods to change the text alignment of a UITextView. I am targeting iOS6 and above. The idea is that the user can change the alignment of the input of the textview. if the input is in English, below methods are working fine. If the inputs in Arabic, It wont change the alignment.

-(void) changeLeftAllignment
{
    myTextView.textAlignment = NSTextAlignmentLeft;
}

-(void) changeRightAllignment
{
    myTextView.textAlignment = NSTextAlignmentRight;
}

这仅适用于英语输入。如果我将键盘语言从右向左更改为阿拉伯语,它将不再起作用。

This works perfectly for only English input. If I change the keyboard language to Arabic which is from right to left, it doesn't work anymore.

有任何想法可以解决这个问题吗?

Any idea to solve this issue?

谢谢

推荐答案

从iOS 6.0+开始,你应该使用 NSTextAlignmentNatural 。不推荐使用枚举的 UITextAlignment 。如果您在iOS 6.0目标上运行代码,它将自动使用自然对齐,并且所有代码都将按预期工作。

From iOS 6.0+ you should use NSTextAlignmentNatural. The UITextAlignment enumerated is deprecated. If you run your code on an iOS 6.0 target, it will automatically use natural alignment and all will work as expected.

在以前版本的iOS中,您可以检查是否语言输入是RTL并反转你的对齐方式:

In previous versions of iOS, you could check whether the language input is RTL and invert your alignment:

- (BOOL)isRTL {
  return ([NSLocale characterDirectionForLanguage:[[NSLocale preferredLanguages] objectAtIndex:0]] == NSLocaleLanguageDirectionRightToLeft);
}

-(void) changeLeftAllignment
{
    myTextView.textAlignment = (![self isRTL]]) ? UITextAlignmentLeft : UITextAlignmentRight;
}

-(void) changeRightAllignment
{
    myTextView.textAlignment = (![self isRTL]]) ? UITextAlignmentRight : UITextAlignmentLeft;
}

这篇关于UITextView对齐“从右到左”的书面语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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