0

Scenario: The user can change the Keyboard language dynamically and enter text into UITextView.

Issue: If the user changes the keyboard language to Arabic and enters text, the text is in Right to Left direction.

Is there a way in which I can determine the writing Direction. Either from UITextView or The Entered Text or Some magic from the Keyboard

Please help me solve this.

Thanks, Roshit

PS: The TextAllignment of UITextView doesnot do the magic..

EDIT: I also convert the NSString from the Textfield into an NSAttributedString and draw Using CoreText. Can I by any chance get the writing direction in this scenario (NSAttributedString or CoreText)

Roshit
  • 1,589
  • 1
  • 15
  • 37

3 Answers3

1

Take a look at the baseWritingDirectionForPosition:inDirection: method of the UITextInput protocol.

Both UITextView and UITextField implement the UITextInput protocol.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
1

Thanks to @rob, this is what I came up with:

UITextPosition *aPosition;
UITextWritingDirection aDirection = [aTextView baseWritingDirectionForPosition:aPosition inDirection:UITextStorageDirectionForward];

switch (aDirection) {
    case UITextWritingDirectionNatural:
        NSLog(@"Natural");
        break;

    case UITextWritingDirectionLeftToRight:
        NSLog(@"L-R");
        break;

    case UITextWritingDirectionRightToLeft:
        NSLog(@"R-L");
        break;

    default:
        break;
}

It worked perfectly fine.

Just out of curiosity.. I used : UITextPosition *aPosition; I have not initialized it and set a position to it. Then how did this work. @rob- Any help on this ?

Roshit
  • 1,589
  • 1
  • 15
  • 37
  • 1
    It's probably just giving you the writing direction of the start of the text. It really depends on the implementation of `UITextView`, which I don't have access to. The normal way to initialize `aPosition` would be using something like the `beginningOfDocument` property or `selectedTextRange.start`. – rob mayoff Mar 01 '12 at 21:37
  • As Rob points out, leaving aPosition uninitialized seems like a bad idea. [aTextView beginningOfDocument] or [aTextView endOfDocument] would at least be a valid and defined value to use as a parameter. You may be able to call positionFromPosition: offset: to get an arbitrary position relative to the beginning. Uninitialized stack variables can contain arbitrary values. – John M. P. Knox Nov 15 '12 at 19:18
0

A quick, and thus in-linable, option is to use the Locale like this:

[NSLocale lineDirectionForLanguage:[[NSLocale currentLocale]languageCode]]

Use it for things like Tab Stops like this:

NSTextTab* tab = [[NSTextTab alloc]initWithTextAlignment:(([NSLocale lineDirectionForLanguage:[[NSLocale currentLocale]languageCode]]==kCFLocaleLanguageDirectionRightToLeft)?NSTextAlignmentRight:NSTextAlignmentLeft) location:100.0f options:@{}];
Linasses
  • 395
  • 4
  • 14