Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UItextView arabic text aligned to right

Using my custom arabic keyboard on UItextView inputView, I m filling my textView with the arabic text but cannot get the written text align to right....Need help to align text to right.

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{

 if(showCustomKeyboard==NO){
 [textView resignFirstResponder];
 textView.inputView=nil;
 [textView becomeFirstResponder];
 return YES; 
}

 else{
     [textView resignFirstResponder];
     if(customKeyboard==nil){
     customKeyboard=[[CustomKeyboard alloc] initWithFrame:CGRectMake(0, 264, 320, 216)];
     [customKeyboard setDelegate:self];
 } 
 if([[UIApplication sharedApplication] respondsToSelector:@selector(inputView)]){
     if (textView.inputView == nil) {
           textView.inputView = customKeyboard;
           [textView becomeFirstResponder];
     }
 } 
    self.customKeyboard.currentField=textView;
    [textView becomeFirstResponder];
 }
 return YES; 
}
like image 892
Khalil Ghaus Avatar asked Mar 26 '12 09:03

Khalil Ghaus


People also ask

Is Arabic text right aligned?

Arabic language is a right to left language (RTL) which means writing starts from the right and continues to the left. However, when text is right-justified explicitly, it should have the same alignment as English.

Can you center align Arabic text?

translated arabic text cannot be aligned properly, arabic is written from right to left, and there was no way to format it properly even top alignment as u can see.


2 Answers

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.

like image 55
Anton Avatar answered Oct 05 '22 23:10

Anton


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.

like image 36
Giuseppe Garassino Avatar answered Oct 06 '22 00:10

Giuseppe Garassino