Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS RTL - improperly displaying English inside RTL string

iOS application, we're to display news, coming from server. UIlabel is used

  • Everything is perfect when he sentence is in a single language only with no regard to layout (we're switching layout RTL to LTR for different languages, including Arabic, Hebrew)
  • When inside LTR language we have RTL words, they break the sentence structure (see the picture, BTN must be in the beginning of the line, but it jumped to the end) English words inside RTL language Any idea how to solve this? Thanks in advance :)
like image 759
Alexey Abraham Avatar asked May 28 '16 09:05

Alexey Abraham


4 Answers

Apple are using "Unicode Bidirectional Algorithm" to present text. If the first character in a string is LTR the algorithm treat the presentation of the rest of the string as LTR. If you know in advance the language of the string RTL you can use the unicode \u200F and \u202c to force the RTL alignment.

Objective-C

[NSString stringWithFormat:@"\u200F%@\u202c", @"your string with RTL content"]

[NSString stringWithFormat:@"\u200E%@\u202c", @"your string with LTR content"]

Swift

String(format: "\u200F%@\u202c", "your string with RTL content")

String(format: "\u200E%@\u202c", "your string with LTR content")
like image 128
Pichirichi Avatar answered Nov 18 '22 02:11

Pichirichi


Here is @Pichirichi solution for swift 5.2 :

"\u{200F}\("your string with RTL content")\u{202c}"

"u200E\("your string with LTR content")\u{202c}"
like image 43
Ali Pishvaee Avatar answered Nov 18 '22 04:11

Ali Pishvaee


Swift 5:

extension String {
    
    func forceUnicodeRTL() -> String {
        return "\u{200F}\(self)\u{200E}"
    }
}
like image 3
Farshid roohi Avatar answered Nov 18 '22 02:11

Farshid roohi


  "arabic text \u200E english text \u200F arabic text \u200E english text"

Solved the issue

like image 2
Alexey Abraham Avatar answered Nov 18 '22 03:11

Alexey Abraham