Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoLayout + RTL + UILabel text alignment

I'm finally getting round to wrestling with Auto Layout and can't seem to figure out how to get right-to-left (RTL) support to work the way I'd expect/want...

I have designed the view in Interface Builder as shown:

IB

With the resulting app running as expected when using English:

English

However when switching to an RTL language (Arabic in this case), the entire view flips (which is great) but the UILabel's text is still left aligned. I'd expect it to be right aligned to keep it up against the UIImageView.

Arabic

Clearly I'm missing something and/or this isn't covered by Auto Layout.

Am I supposed to set the textAlignment manually when using an RTL language?

like image 753
Steve Wilford Avatar asked Sep 11 '13 14:09

Steve Wilford


1 Answers

You want NSTextAlignmentNatural. That infers the text alignment from the loaded application language (not from the script).

For iOS 9 and later (using Xcode 7), you can set this in the storyboard (choose the --- alignment option). If you need to target earlier releases, you'll need to create an outlet to the label and set the alignment in awakeFromNib.

- (void)awakeFromNib {     [[self label] setTextAlignment:NSTextAlignmentNatural]; } 
like image 135
Ken Avatar answered Oct 05 '22 19:10

Ken