UItextView arabic text aligned to right

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 little tricky because UITextView supports having different parts of the text with different writing directions. In my case, I used [someTextView textRangeFromPosition:[someTextView beginningOfDocument] toPosition:[someTextView endOfDocument]] to select the full text range of the UITextView. You can adjust that part if your needs are different.

You may also want to check whether the text in your UITextView is LTR to RTL. You can do that with this:

if ([someTextView baseWritingDirectionForPosition:[someTextView beginningOfDocument] inDirection:UITextStorageDirectionForward] == UITextWritingDirectionLeftToRight) {
    // do something...
}

Note that I specified the start of the text using [someTextView beginningOfDocument] and searched forward using UITextStorageDirectionForward. Your needs might differ.

If you subclass UITextView replace all these code samples with "self" and not "someTextView", of course.

I recommend reading about the UITextInput protocol, to which UITextView conforms, 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 together with setting the base writing direction, you will have issues because RTL text when aligned left in a UITextView actually aligns to the right visually. Setting text with an RTL writing direction to align right will align it to the left of the UITextView.


Try textAlignment property.

textView.textAlignment = UITextAlignmentRight;

Take a look at UITextView Class Reference.

EDIT: Maybe CATextLayer can help you, someone suggests to use this class to customize text, but I've never used it personally...

Otherwise, you can force your textView to reverse your input in UITextFieldDelegate method:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

The text field calls this method whenever the user types a new character in the text field or deletes an existing character. Here you can replace your input with a new NSString where you put the characters from right to left.

Hope this makes sense...

Ah... Do not forget to set

textView.textAlignment = UITextAlignmentRight;

to move your prompt on the right.