8

I have implemented Language Localization in my iOS App. So, now user can set the Arabic Language in my iPad Application.

I am getting the localized string response from the server and now I want to set this localized string to my UILabel in Right To Left format with Right Alignment.

Update :

My server response is already in RTL format. Now, I just wanted to set the text alignment to right when I have Arabic text in my UILabel.

Right now I have to write code to set the alignment of UILabel based on the language.

So, I just want to know if there is any property available for UILabel by setting which I can make the text of UILabel Right Aligned in case of Arabic Language.

Bhavin
  • 27,155
  • 11
  • 55
  • 94
  • please apply arabic font . maybe it will solve your issue. – i-Maddy Feb 25 '14 at 07:38
  • 1
    Try this: http://stackoverflow.com/questions/18744447/autolayout-rtl-uilabel-text-alignment – Dimitris Bouzikas Feb 25 '14 at 07:40
  • If you set the `attributedText` property for the label, you can apply a mutable paragraph style with the *RTL* `baseWritingDirection` set. But, the label should apply the correct writing direction to the text. If you're using constraints, make sure you have your Leading and Trailing constraints set up correctly. – Jason Coco Feb 25 '14 at 07:41
  • **I want all my labels to be**, You mean in whole project ? – Kumar KL Feb 25 '14 at 08:49
  • check the language like in the question http://stackoverflow.com/questions/6325073/detect-language-of-nsstring?lq=1#13783833 and if it is Arabic change the label's alignment. – Khaled Annajar Dec 10 '15 at 11:14

5 Answers5

6

For Swift3 //MARK: UILabel extension

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

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

to use this add following with your label :

detailLabel.text = details[0]

detailLabel.decideTextDirection()
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
JaspreetKour
  • 777
  • 9
  • 11
4

have you try this.

 [[self label] setTextAlignment:NSTextAlignmentNatural];

AutoLayout + RTL + UILabel text alignment

Hope this will solve your problem.

Community
  • 1
  • 1
Ayaz
  • 1,398
  • 15
  • 29
  • 8
    No, this will not do what the question is asking for. Natural alignment right- or left-aligns text based on the language the application is running in, *not* based on the script of the text in the label. The question specifically asks about an app which has an internal language switcher, so this will not work. – lensovet Jul 25 '15 at 22:53
2

Display the text normally as you do in english if you are getting arabic text from server no need to align it. Just right align the text.

Tushar
  • 88
  • 7
  • 1
    That's what I am doing. I just wanted to know if there is any property available for `UILabel` which can set the Alignment of `UILabel` to right if it gets the Arabic Text. Right now I have to write a code to set the alignment based on the language. I just want to know if there is any simpler way using which `UILabel` can set its alignment. – Bhavin Feb 25 '14 at 09:26
  • 1
    @Tushar : did you find solution for this? – Fahim Parkar Oct 04 '15 at 07:21
1

Make your text label the entire width of the view. Then the text will right align from the right side of the screen.

Matt H
  • 6,422
  • 2
  • 28
  • 32
0

For anyone wanting an example using the NaturalLanguage library (NSLinguisticTagger is deprecated). This builds on @jaspreetkour's answer, and also works with attributed text.

extension UILabel {

    func decideTextDirection () {
        let recognizer = NLLanguageRecognizer()
        var txt = self.text ?? ""
        if txt.isEmpty {
            // check attributed text
            txt = self.attributedText?.string ?? ""
        }
        if !txt.isEmpty {
            recognizer.processString(txt)
            if let language = recognizer.dominantLanguage {
                switch language {
                case .arabic:
                    self.textAlignment = .right
                default:
                    self.textAlignment = .left
                }
            }
        }
    }
}
Josh Kopecek
  • 143
  • 10