Alexey Abraham
Alexey Abraham

Reputation: 398

iOS RTL - improperly displaying English inside RTL string

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

Upvotes: 10

Views: 4368

Answers (5)

Farshid roohi
Farshid roohi

Reputation: 768

Swift 5:

extension String {
    
    func forceUnicodeRTL() -> String {
        return "\u{200F}\(self)\u{200E}"
    }
}

Upvotes: 4

Pichirichi
Pichirichi

Reputation: 1510

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")

Upvotes: 17

Ali Pishvaee
Ali Pishvaee

Reputation: 302

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

Upvotes: 5

Alexey Abraham
Alexey Abraham

Reputation: 398

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

Solved the issue

Upvotes: 2

Alexey Abraham
Alexey Abraham

Reputation: 398

Some info from here is helpful:

[[self label] setTextAlignment:NSTextAlignmentNatural];

AutoLayout + RTL + UILabel text alignment

But sometimes it still not working as expected

Upvotes: 0

Related Questions