27

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?

Steve Wilford
  • 8,894
  • 5
  • 42
  • 66
  • I don't think AutoLayout changes internal states of the elements it lays out. In this case, yes - the best bet would be that you need to do this manually. – Stavash Sep 11 '13 at 14:51
  • can you tell me how did you flip the whole view using Auto Layout ? – JAHelia May 05 '14 at 13:57
  • @JAHelia it is done automatically by the system when changing to a language that is right-to-left. – Steve Wilford May 05 '14 at 15:46
  • @SteveWilford I just use the base storyboard, no additional storyboard for arabic, how does your Xcode know that it should flip the view ? did you design the flipped interface in another Arabic localized storyboard or you just use 1 storyboard for both English & Arabic ? – JAHelia May 06 '14 at 06:20
  • It's all in a single storyboard. I believe Xcode configures it to flip for RTL by default. But make sure you have "Respect language direction" checked on the leading constraint's attributes inspector. – Steve Wilford May 22 '14 at 10:12
  • How you doing mirroring the UI? If doing apart from changing the language.Please, let me know. – Sanoj Kashyap Jun 30 '14 at 12:43
  • It's provided by Auto Layout, when you change the language in the Settings app to an RTL language it will mirror the UI. – Steve Wilford Jul 01 '14 at 08:33
  • what was the solution for this? – Nick Ginanto Jun 21 '15 at 12:38
  • @NickGinanto it depends on your specific requirements, if you're happy to have the width of the label based on it's intrinsic content size then [DarthMike's answer](http://stackoverflow.com/a/18746363/2613662) is the best (and I've actually been tempted to accept that). However if you need the width of the label to remain as shown you'd need to go down the route of NSTextAlignmentNatural as mentioned in both [Ken's answer](http://stackoverflow.com/a/18748791/2613662) and [my own](http://stackoverflow.com/a/18759288/2613662). I haven't tried it on iOS 7+ (I go with intrinsicContentSize). – Steve Wilford Jun 22 '15 at 06:15
  • thanks, in IB it seems the only way to set natural alignment is to use attributed string – Nick Ginanto Jun 22 '15 at 07:19
  • Same type of requirement is this mine. How do i do this from autolayout – Jitendra Jul 07 '15 at 06:21
  • how to achieve using autoresize or without autolayout – 9to5ios Dec 28 '16 at 17:23

10 Answers10

34

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];
}
lensovet
  • 5,490
  • 2
  • 23
  • 19
Ken
  • 12,933
  • 4
  • 29
  • 32
  • 1
    This sounds promising. I'll give this a try in the morning. – Steve Wilford Sep 11 '13 at 20:08
  • I'm accepting this as it pointed me in the right direction, but it isn't quite this simple. See my answer for details. – Steve Wilford Sep 12 '13 at 08:33
  • 1
    Xcode 7 allows setting natural alignment on labels. – progrmr Jun 13 '15 at 20:46
  • A little improvement in the answer: Check for Center-Align text... if (self.textAlignment != NSTextAlignmentCenter) { [self setTextAlignment:NSTextAlignmentNatural]; } – aqavi_paracha Jul 24 '15 at 13:17
  • Rectification: setTextAlignment should be invoked after awakeFromNib (e.g. in viewDidLoad), otherwise, the UILabel properties defined at the storyboard level will be applied (and therefore override whatever you defined...) – Ariel Malka Dec 29 '15 at 16:31
  • Where you want the text to be center align, it will change that also to right align. – M Afham Aug 11 '20 at 09:11
13

For me those solutions didn't help, and I ended up doing something pretty ugly but it's the only one that did the trick for me. I added it as an NSString category:

NSString+Extras.h:

#import <Foundation/Foundation.h>

@interface NSString (Extras)
- (NSTextAlignment)naturalTextAligment;
@end

NSString+Extras.m:

#import "NSString+Extras.h"

@implementation NSString (Extras)

- (NSTextAlignment)naturalTextAligment {
    NSArray *tagschemes = [NSArray arrayWithObjects:NSLinguisticTagSchemeLanguage, nil];
    NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes:tagschemes options:0];
    [tagger setString:self];
    NSString *language = [tagger tagAtIndex:0 scheme:NSLinguisticTagSchemeLanguage tokenRange:NULL sentenceRange:NULL];
    if ([language rangeOfString:@"he"].location != NSNotFound || [language rangeOfString:@"ar"].location != NSNotFound) {
        return NSTextAlignmentRight;
    } else {
        return NSTextAlignmentLeft;
    }
}
@end

To detect the language I used this SO answer.

Community
  • 1
  • 1
Aviel Gross
  • 9,770
  • 3
  • 52
  • 62
  • "Natural" alignment only works if you are letting the OS load the correct localization for your app based on system-wide user settings and you have ar.lproj or he.lproj in your app. If you have an in-app language picker, natural won't work, and your code is needed. – lensovet Jul 25 '15 at 22:57
  • Well, @lensovet, "Natural" means the way nature meant. And nature meant for heb/ar to be right-to-left. The system tries to do it with `setTextAlignment:NSTextAlignmentNatural` but from my experience it doesn't work as it should, and my solution, albeit not "clean", works; it aligns the text in it's natural way, regardless of the system language. What you want is something like "system-lang alignment", which is not "natural alignment". – Aviel Gross Jul 26 '15 at 07:35
  • You're welcome to file a bug report requesting that. All I'm saying is that's how it works today. It's based on the UI language of the app, not the contents of the text. – lensovet Jul 26 '15 at 17:45
5

Follow up from Ken's answer

Setting textAlignment to NSTextAlignmentNatural is not possible on UILabel, it will result in an exception getting thrown:

Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'textAlignment does not accept NSTextAlignmentNatural'

It does work when using attributed text and this can be set in Interface Builder as shown:

attributed text natural alignment

However, it would appear that attributed text is not picked up when localising the storyboard.

To get around this I have left the UILabel configured as plain in Interface Builder and created an NSAttributedString with the label's text, set the alignment on the attributed string and assign it to the label's attributedText property:

-(void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentNatural;

    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self.lbl.text];
    [string addAttribute:NSParagraphStyleAttributeName
                   value:paragraphStyle
                   range:NSMakeRange(0, string.length)];

    self.lbl.attributedText = string;
}

This works fine in this simple case but I can see it falling over when you need more complex attributed string styling. But obviously in that case you'd probably just be using NSLocalizedString or equivalents when creating the NSAttributedString.

Steve Wilford
  • 8,894
  • 5
  • 42
  • 66
  • This sounds like a bug. Have you filed one yet? I’d definitely dupe it! – danyowdee Sep 12 '13 at 09:17
  • 1
    I haven't filed a bug as it's mentioned in the release notes for 6.1, right at the bottom: https://developer.apple.com/library/ios/releasenotes/General/RN-iOSSDK-6_1/#//apple_ref/doc/uid/TP40012869-CH1-SW21 – Steve Wilford Sep 12 '13 at 09:19
  • Nice catch! But since it’s a severe limitation, I’d be tempted to call it bug even though enhancement request would be more suiting ;-) – danyowdee Sep 25 '13 at 07:07
  • 4
    It no longer crashes in iOS 7, but it doesn't seem to right align RTL text :/ – Steve Wilford Sep 25 '13 at 08:22
  • @SteveWilford Hence, me visiting this question. I think you have to set the alignment **after** setting the text. Have you figured it out? – Mazyod Mar 05 '14 at 06:47
  • 1
    This snippet + defining my `UILabel` to attributed in my storyboard doesn't seem to work on iOS7 with Hebrew text... Any ideas? (I have made sure the text is available and is correct with hebrew chars only at time of running these lines in my `viewWillAppear`) – Aviel Gross Apr 10 '14 at 12:04
  • @SteveWilford is this working for you on iOS 8 or iOS 7? – Pichirichi Sep 14 '14 at 14:51
  • Natural alignment is based on the UI language of the app, not the content of the label. – lensovet Jul 26 '15 at 19:05
  • @lensovet, what does "the UI language of the app" mean? – Adil Hussain Oct 07 '19 at 15:20
  • I can confirm that this solution by Steve Wilford does not work in iOS 13. For a super simple iOS application which demonstrates this please see [here](https://github.com/adil-hussain-84/uilabel-natural-text-alignment). – Adil Hussain Oct 07 '19 at 15:22
  • I did some experimentation and figured out that what @lensovet meant by "the UI language of the app" was the "the language of the device". So when the `UILabel`'s text alignment is set to natural and the device's language is set to English then the text in the `UILabel` will left-align regardless of its content. And when the `UILabel`'s text alignment is set to natural and the device's language is set to Arabic then the text in the `UILabel` will right-align. For a super simple application which demonstrates this see [here](https://github.com/adil-hussain-84/uilabel-natural-text-alignment). – Adil Hussain Oct 11 '19 at 08:14
5

@Aviel answer as a swift UILabel extension

//MARK: UILabel extension
extension UILabel {
    func decideTextDirection () {
        let tagScheme = [NSLinguisticTagSchemeLanguage]
        let tagger    = NSLinguisticTagger(tagSchemes: tagScheme, options: 0)
        tagger.string = self.text
        let lang      = tagger.tagAtIndex(0, scheme: NSLinguisticTagSchemeLanguage,
            tokenRange: nil, sentenceRange: nil)

        if lang?.rangeOfString("he") != nil ||  lang?.rangeOfString("ar") != nil {
            self.textAlignment = NSTextAlignment.Right
        } else {
            self.textAlignment = NSTextAlignment.Left
        }
    }
}

How to use it ?

label.text = "كتابة باللغة العربية" // Assign text
label.decideTextDirection()          // Decide direction
Husam
  • 8,149
  • 3
  • 38
  • 45
4

I think you don't want to use text alignment in this case, for a label.

You can just let the width be determined by intrinsicContentSize, and remove any width constraints on the label. You will achieve the desired effect of the label text aligned to the view.

For x axis, you only need this constraint between label and imageview: [imageview]-[label]

This is only a horizontal spacing constraint. No leading or trailing to superview.

DarthMike
  • 3,471
  • 1
  • 22
  • 18
  • Well, you'll want to add a ≥ standard trailing constraint to make sure the text doesn't run off the edge of the screen, which can happen with larger fonts/dynamic type/other strings. But yes, that is a legitimate workaround for apps targeting iOS 8 or older. – lensovet Jul 25 '15 at 22:56
3

@Aviel answer as a swift 4 UILabel extension

extension UILabel {
    func decideTextDirection () {
        let tagScheme = [NSLinguisticTagScheme.language]
        let tagger    = NSLinguisticTagger(tagSchemes: tagScheme, options: 0)
        tagger.string = self.text
        let lang      = tagger.tag(at: 0, scheme: NSLinguisticTagScheme.language,
                                          tokenRange: nil, sentenceRange: nil)

        if lang?.rawValue.range(of: "he") != nil ||  lang?.rawValue.range(of: "ar") != nil {
            self.textAlignment = NSTextAlignment.right
        } else {
            self.textAlignment = NSTextAlignment.left
        }
    }
}

usage

label.text = "كتابة باللغة العربية" // Assign text
label.decideTextDirection()          // Decide direction
Hamish
  • 1,685
  • 22
  • 37
1

This function helped me out

-(void)fnForWritingDirection:(UILabel*)label textFor:(NSString *)stringForText{

    NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString: [stringForText stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    [paragraphStyle setBaseWritingDirection:NSWritingDirectionRightToLeft];

    [attrStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attrStr length])];

    label.attributedText=attrStr;

}
Sukeshj
  • 1,503
  • 1
  • 19
  • 25
  • 1
    You should never force the base writing direction of text unless absolutely necessary. In this case, it isn't. – lensovet Jul 25 '15 at 22:55
1

this worked for me:

extension UILabel {
    func decideTextDirection () {
        let tagScheme = [NSLinguisticTagScheme.language]
        let tagger = NSLinguisticTagger(tagSchemes: tagScheme, options: 0)
        tagger.string = self.text
        let lang = tagger.tag(at: 0, scheme: NSLinguisticTagScheme.language, tokenRange: nil, sentenceRange: nil)
        let langDir = NSLocale.characterDirection(forLanguage: lang?.rawValue ?? "en")
        if langDir == .rightToLeft { self.textAlignment = NSTextAlignment.right }
        else { self.textAlignment = NSTextAlignment.left }
    }
}

usage:

nameLabel.text = "مهدی"
nameLabel.decideTextDirection()
Mahdi Moqadasi
  • 2,029
  • 4
  • 26
  • 52
0

You can use MyLinearLayout to easy support RTL and LRT.

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
欧阳大哥
  • 19
  • 1
  • 1
  • There's nothing in the README of the repository which you linked which explains how it makes supporting RTL and LRT easier. Please explain why you linked this repository and how it relates to the original question in this thread. – Adil Hussain Oct 07 '19 at 14:57
0

Here is my version. It's simpler and also handles multiple languages in the source document.

The Main point is to use the dominantLanguage:

let lang = tagger.dominantLanguage

Code Snippet:

 extension UILabel {
    func determineTextDirection () {
        guard self.text != nil else {return}

        let tagger = NSLinguisticTagger(tagSchemes: [.language], options: 0)
        tagger.string = self.text

        let lang = tagger.dominantLanguage

        let rtl = lang == "he" || lang == "ar"

        self.textAlignment = rtl ? .right : .left
    }
}

Usage:

titleLabel.text = "UILabel היפוך שפה עבור"
titleLabel.determineTextDirection()

Finally: Note that if the App is localized and you may rely on the phones language - the solution your after is: "Natural Text Alignment for RTL": i.e:

titleLabel.textAlignment = .natural

enter image description here

Use the NSLinguisticTagger when your app shows multiple lines with different languages. or when you allow free search in any language, regardless of the local.

TomerBu
  • 1,483
  • 15
  • 15