Search code examples
iosuitextview

How to know the direction of the text?


I need to know the direction of a text depending on the language of the keyboard when typing in a UITextView. For example, if the user is typing in english, then the direction will be "left to right" and if the user is typing in arabic, "right to left". I need that value without doubts and avoiding to hardcoding a matching between each language and its text direction.

Is this possible?


Solution

  • Check this out this method from UITextInput Protocol:

    - (UITextWritingDirection)baseWritingDirectionForPosition:(UITextPosition *)position inDirection:(UITextStorageDirection)direction
    

    This returns a constant that represents a writing direction (for example, left-to-right or right-to-left)

    UITextView implements such protocol.