UItextView Arabic Text Aligned Right

Using my regular Arabic keyboard in the UItextView I / O interface, I fill my text with Arabic text, but I can’t get the text aligned according ... I need help to align the text to the 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; } 
+6
source share
6 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 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) { // do something... } 

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.

+16
source

Try the textAlignment property.

 textView.textAlignment = UITextAlignmentRight; 

See the UITextView Class Reference .

EDIT: Perhaps CATextLayer can help you, someone suggests using this class to customize text, but I never used it personally ...

Otherwise, you can force your text file to change your entry in the UITextFieldDelegate method:

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

A text field calls this method whenever a user enters a new character in a 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 ... Remember to install

textView.textAlignment = UITextAlignmentRight;

to move the invitation to the right.

+4
source

Try this code:

 yourtextview.textAlignment = UITextAlignmentRight; 

Hope this helps you.

+1
source

Something that no one mentioned here or about any other post is that you did not call sizeToFit on the TextView. It just aligns the text (not the text) to the left, which gives the illusion that the text remains on the right, and not right to left.

0
source

If you are creating a user interface from a Storyboard, the set limit on Lead or Trailing space and the value of First Item will be Respect Language Direction

0
source

in fast you can use this code

 textView.makeTextWritingDirectionRightToLeft(true) 
0
source

Source: https://habr.com/ru/post/911656/


All Articles