16

I want to set a label to string: "خخخ just bought: Disguise Kit." but when I run the test, the label show ".just bought: Disguise Kit خخخ"?

If the text is not begin with Arabic, It will show as what I set. What's the problem?

Does anybody know how to deal with this issue?

user1300503
  • 305
  • 3
  • 10
  • Is this a RTL problem? Can you encourage the arabic to display LTR? – James Webster Sep 11 '12 at 14:01
  • @JamesWebster I don't think so. if it is RTL problem, why just for this case which begin with Arabic word? – user1300503 Sep 11 '12 at 14:27
  • Because the sentence will begin RTL, (خخخ starts right heading left), then it appears to swap to LTR to draw the remaining words. – James Webster Sep 11 '12 at 15:00
  • @JamesWebster Could you give an example how to fix? Because if you set Label.textAlignment = UITextAlignmentRight; it is just show the text right align, instead of revert the string order. – user1300503 Sep 12 '12 at 05:21
  • I'm afraid I don't know how to fix it. I just recognised that it might be this problem. I haven't really worked with RTL languages. – James Webster Sep 12 '12 at 07:00

3 Answers3

43

First, read Cal Henderson's excellent "Understanding Bidirectional (BIDI) Text in Unicode.".

Believe it or not, UILabel is laying it out the way you asked. You've provided a right-to-left string (starting in Arabic). It starts displaying that right to left. You then embedded a left-to-right English string, which it lays out left to right. It then sees the period, and lays that out right-to-left since this is an Arabic string that just happens to have some English in it (as best UILabel can tell).

What you meant to have is a left-to-right string with Arabic embedded. That means you have to start the string with left-to-right characters. Two options: add some English to the start, or use the zero-width Left-to-Right Mark (U+200E, LRM) to anchor the beginning of the string into LTR mode.

Objective-C:

self.label.text = @"\u200eكتب just bought: Disguise Kit.";

Swift:

self.label.text = "\u{200E}كتب just bought: Disguise Kit."

The good news about U+200E is that you can safely add it to every LTR string before you display it. You can even safely put it at the start of your localized strings for LTR languages. If it's redundant, it doesn't hurt anything.

A couple of other things to note: never test this stuff with ككك, always test with كتب (like every good student :D) or better yet something like الو. Otherwise you can't tell when the Arabic is laid out backwards. I like الو because it looks wildly different backwards.

Also, when testing, note that Xcode doesn't know how to layout Arabic. So if you write any static strings in your code, they'll be displayed backwards in the editor, but they'll display correctly in the UI. Drives me crazy.


Be sure to also see guru_meditator's answer for helpful related features added in iOS 10+/macOS 10.12.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
5

Since iOS 10 (and macOS 10.12) String localizedStringWithFormat inserts Unicode isolates around placeholders. This is a higher level way to format strings with mixed language direction, without manually inserting directional marks.

String.localizedStringWithFormat("%@ just bought: Disguise Kit.", "خخخ")
// "⁨خخخ⁩ just bought: Disguise Kit."

Compare to:

String(format: "%@ just bought: Disguise Kit.", "خخخ")
// ".just bought: Disguise Kit خخخ"

To see what localizedStringWithFormat is doing:

let scalars = String.localizedStringWithFormat("%@ just bought: Disguise Kit.", "خخخ")
    .unicodeScalars.map { "U+\(String(format: "%04X", $0.value))" }
print(scalars)
// ["U+2068", "U+062E", "U+062E", "U+062E", "U+2069", "U+0020", ...

Where U+2068 is FIRST STRONG ISOLATE, and U+2069 is POP DIRECTIONAL ISOLATE. You can read more about isolates in: https://www.unicode.org/reports/tr9/tr9-41.html#Explicit_Directional_Isolates

This feature was introduced in WWDC 2016 session 232 What's New in International User Interfaces

guru_meditator
  • 549
  • 6
  • 11
0

Actually, \u{200E} (or any) symbol can be inserted into the string literal.

  1. Add "Unicode Hex Input" (System Preferences -> Keyboard -> Input Sources -> + -> Unicode Hex Input).
  2. In the text editor move the cursor to the place where you want to insert a symbol.
  3. Switch input source to the "Unicode Hex Input".
  4. Hold the Option key and type 200E.
  5. The symbol should be inserted and the string should look like you expect.
Andrey
  • 521
  • 6
  • 9