问题
Using iOS 5.0+ for iPad, I have an app with containing Arabic text in a UITextView where:
myTextView.editable = NO;
Strangely, the Arabic text suddenly aligns to the left and not the right, causing quite a problem for user readability.
I appreciate any help overcoming this strange behavior!
回答1:
Solved it! Turns out it's very simple: just set the textAlignment to UITextAlignmentRight.
UITextView works differently when editable and not, especially when it comes to RTL text. If the base writing direction is RTL in an editable text view, you must align the text left, not right, in order for the text to actually align right (the RTL writing direction flips the defaults!)
So, when you have a UITextView, you may want to first check the editable property and then use the writing direction of the first character (this is how iOS determines whether the text is aligned left or right) to set the textAlignment property.
For example:
// 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;
}
}
UPDATE FOR iOS 6:
In iOS 6, a UITextView's textAlignment property actually corresponds to its appearance on the screen. For iOS 6, just set the textAlignment to the direction you want to see it. The above code works as described for iOS 5.1 and earlier.
I hope this helps anyone else dealing with this issue!
来源:https://stackoverflow.com/questions/10859471/non-editable-uitextview-left-aligns-rtl-arabic-hebrew-etc-text