8

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; 
}
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
Khalil Ghaus
  • 251
  • 1
  • 3
  • 15
  • Please Post your code. so that we can help you. – Parth Bhatt Mar 26 '12 at 11:17
  • -(BOOL)textViewShouldBeginEditing:(UITextView *)textView{ if(showCustomKeyboard==NO){ [textView resignFirstResponder]; textView.inputView=nil; [textView becomeFirstResponder]; return YES; } – Khalil Ghaus Mar 26 '12 at 12:18
  • 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; } – Khalil Ghaus Mar 26 '12 at 12:18
  • i have put textView.textAlignment = UITextAlignmentRight; but not working – Khalil Ghaus Mar 26 '12 at 12:19

6 Answers6

17

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.

Anton
  • 3,998
  • 25
  • 40
3

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.

Giuseppe Garassino
  • 2,272
  • 1
  • 27
  • 47
  • Mmm... I have tried to set textView.textAlignment = UITextAlignmentRight in my customKeyboard class: the prompt is on the right, but the text is readable from left to right and not from right to left. Try to take a look at my EDIT. – Giuseppe Garassino Mar 26 '12 at 18:00
  • basically when creating my own custom keyboard and calling it on textview.inputvew, UITextViewDelegate's method are not being called and and if I myself called the method it perform the same behaivour.TextView.inputView is not calling textview delegate method – Khalil Ghaus Mar 28 '12 at 05:38
  • Could you implement your own delegate for custom keyboard? – Giuseppe Garassino Mar 28 '12 at 14:26
  • I had implemented the delegate but no use...Is there any tutorial for creating a custom keyboard library like UIKeyboard...... – Khalil Ghaus Mar 30 '12 at 11:32
1

Try this code:

yourtextview.textAlignment = UITextAlignmentRight;

Hope this helps you.

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
Ravi Kumar Karunanithi
  • 2,151
  • 2
  • 19
  • 41
  • I have create a Custom Keyboard for arabic through UIviews and UIbuttons.And called it on -(void)textViewDidBegin after this wheni press buttons and called the delegate method it started aligning the text from left not from right – Khalil Ghaus Mar 26 '12 at 12:10
0

Something which no one mentioned here or on any other post is that make sure you have not called sizeToFit for TextView. It simple aligns the textView (not text) to the left which gives the illusion that text is left to right instead of right to left.

Waqar Ahmed
  • 258
  • 1
  • 5
  • 16
0

If you are creating UI from Storyboard, the set constraint to Lead or Trailing space and value of First Item will be Respect Language Direction

Arup Sarker
  • 173
  • 1
  • 9
0

in swift you can use this code

textView.makeTextWritingDirectionRightToLeft(true)
Masoud Roosta
  • 455
  • 9
  • 19