Make the text UILabel Right Aligned if the text is written in Arabic

I applied Language Localization in my iOS application. So, now the user can install the Arabic language in my iPad application.

I get a localized string response from the server, and now I want to set this localized string to mine UILabelin right-to-left format using Right Alignment .

Update:

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

Now I have to write code to set UILabellanguage-based alignment .

So, I just want to know if there is any property for UILabelsetting that I can make the text UILabelRight Aligned in the case of the Arabic language.

+3
source share
4 answers

Display the text, as usual, in English, if you receive Arabic text from the server, there is no need to align it. Just align the text to the right.

+2
source

Try it.

 [[self label] setTextAlignment:NSTextAlignmentNatural];

AutoLayout + RTL + text alignment UILabel

Hope this solves your problem.

+1
source

. .

+1

Swift3 //MARK: UILabel

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
    }
}

:

detailLabel.text = details[0]

detailLabel.decideTextDirection()
+1

All Articles