You can set the writing direction of a UITextView using the setBaseWritingDirection selector:
UITextView *someTextView = [[UITextView] alloc] init]; [someTextView setBaseWritingDirection:UITextWritingDirectionLeftToRight forRange:[someTextView textRangeFromPosition:[someTextView beginningOfDocument] toPosition:[someTextView endOfDocument]]];
The code is a bit complicated because UITextView supports different parts of the text with different writing directions. In my case, I used [someTextView textRangeFromPosition: [someTextView beginOfDocument] toPosition: [someTextView endOfDocument]] to select the full text range of the UITextView. You can adjust this part if your needs are different.
You can also check if the text is in your UITextView LTR RTL. You can do this with this:
if ([someTextView baseWritingDirectionForPosition:[someTextView beginningOfDocument] inDirection:UITextStorageDirectionForward] == UITextWritingDirectionLeftToRight) {
Notice that I indicated the beginning of the text using [someTextView beginOfDocument] and searched forward using the UITextStorageDirectionForward. Your needs may vary.
If you subclass UITextView, replace all these code samples with "self" and not with "someTextView", of course.
I recommend reading the UITextInput protocol that UITextView corresponds to at http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextInput_Protocol/Reference/Reference.html .
Warning about using the textAlignment property in iOS 5.1 or earlier: if you use it with this approach along with setting the direction of the base record, you will have problems because the RTL text when aligned to the left in UITextView is actually aligned to the right. Setting the text with the RTL recording direction to right justify aligns it to the left edge of the UITextView.
source share