应用语言为英文,左图为UITextfield
s内容左对齐..这很正常,但是当用户选择RTL/阿拉伯语输入语言时,字段对齐方式会自动翻转,如何不考虑输入语言方向强制左对齐?
编辑:
我试过了,并不能解决问题
let _beginningOfDocument = fieldPassword!.beginningOfDocument
let _endOfDocument = fieldPassword!.endOfDocument
fieldPassword!.setBaseWritingDirection(.leftToRight, for: fieldPassword!.textRange(from: _beginningOfDocument , to: _endOfDocument )! )
最佳答案
老实说,我尝试了所有这些,但对我不起作用,因为它与 View 层次结构有关。欲了解更多信息,您可以阅读此 note
Note iOS applies appearance changes when a view enters a window, it doesn’t change the appearance of a view that’s already in a window. To change the appearance of a view that’s currently in a window, remove the view from the view hierarchy and then put it back.
所以试试这个,它对我有用:
extension UITextField {
open override func awakeFromNib() {
super.awakeFromNib()
if Utilities.getCurrentLanguage() == "ar" {
if textAlignment == .natural {
self.textAlignment = .right
}
}
}
}
顺便说一句,“Utilities.getCurrentLanguage()”是一个获取当前语言的函数。
关于ios - 当输入语言为 RTL/Arabic 时,如何防止 UITextfield 更改对齐方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54045150/