NSTextAlignment inverse of .Natural
Asked Answered
S

4

13

I understand that by using NSTextAlignment.Natural you allow a label draw it's content using the default righting direction for a given language, which helps heaps with localisation, allowing the label to draw it's text from left-to-right in languages like english/spanish/etc..., and from right-to-left in languages like arabic. Now my question is that I want to have a label draw in the opposite direction to .Natural, so in a left-to-right language the text should align to the right and in right-to-left languages it should align to the left.

I cannot find an NSTextAlignment enumeration option for this. so I was hoping somebody would give me some advice on how to accomplish this?

Many thanks!

Selachian answered 27/8, 2015 at 9:44 Comment(4)
Why would you want to do that??Unthinkable
Say I have two labels side by side in a cell, one displaying the name of a product and the other it's price. It makes sense that it's alignments should be opposite.Selachian
So is this an objective-c question or a swift question?Wolk
Either or, the answer can be in both Obj-C or Swift.Selachian
J
13

Heaven knows why Apple don't offer a NSTextAlignmentNaturalInverse or equivalent.

Here is the code we use to achieve this in our producation app...

+ (BOOL)isLanguageLayoutDirectionRightToLeft
{
    return [UIApplication sharedApplication].userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft;
}

someLabel.textAlignment = [UIApplication isLanguageLayoutDirectionRightToLeft] ? NSTextAlignmentLeft : NSTextAlignmentRight;
Jordain answered 5/2, 2016 at 13:46 Comment(1)
This approach can't be used in extension bundle likes today widget since there is no UIApplication.Seljuk
F
14

Thanks you @Oliver, here is what I ended up using in swift:

Extension

extension NSTextAlignment {
    static var invNatural: NSTextAlignment {
        return UIApplication.shared.userInterfaceLayoutDirection == .rightToLeft ? .left : .right
    }
}

Usage

myLabel.textAlignment = .invNatural
Footless answered 18/12, 2017 at 8:58 Comment(1)
Very nice trick with static var to "extend" enum's cases. I like it :)Beka
J
13

Heaven knows why Apple don't offer a NSTextAlignmentNaturalInverse or equivalent.

Here is the code we use to achieve this in our producation app...

+ (BOOL)isLanguageLayoutDirectionRightToLeft
{
    return [UIApplication sharedApplication].userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft;
}

someLabel.textAlignment = [UIApplication isLanguageLayoutDirectionRightToLeft] ? NSTextAlignmentLeft : NSTextAlignmentRight;
Jordain answered 5/2, 2016 at 13:46 Comment(1)
This approach can't be used in extension bundle likes today widget since there is no UIApplication.Seljuk
G
1

I do no think that there is a text alignment "unnatural".

But you can retrieve the natural language direction from NSLocale with +characterDirectionForLanguage:. Having this, you can set set the alignment yourself.

NSLocale *locale = [NSLocale currentLocale];
UInt dir = [NSLocale characterDirectionForLanguage:locale.localeIdentifier];
NSLog(@"%d", dir); // returns 1 for left-to-right on my computer (German, de_DE) Hey, that should be ksh_DE!
Girth answered 27/8, 2015 at 10:38 Comment(4)
You shouldn't do something manually which is already done automatically. This can get very confusing with a bigger GUI.Unthinkable
1. Please read the Q including the answer to your Q. 2. Please read the Q: "I want to have a label draw in the opposite". Draw, not layout text. 3. My answer uses a documented API.Girth
You can't draw in a text view -.- Also my answer is officially documented as wellUnthinkable
1. Of course you can draw there. But this is pointless, because you can have a more complex layout depending on the writing direction. 2. I did not say, that your answer uses undocumented or "manual" features. I said that it is not an answer to his Q.Girth
C
1

In Apple's Internationalization and Localization Guide, under Supporting Right-to-Left Languages, they indicate that getting the layout direction should be done using UIView's semanticContentAttribute. So you could implement something like the following:

extension UIView {
    var isRightToLeftLayout: Bool {
        return UIView.userInterfaceLayoutDirection(for: self.semanticContentAttribute) == .rightToLeft
    }
}

This has the advantage of not relying on things like UIApplication, which depending on whether you're working in an extension or not (eg the Today widget), you might not have access to.

Coper answered 19/4, 2019 at 17:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.