In Xcode, how to display text merging English+Arabic and beginning with Arabic?

后端 未结 3 375
深忆病人
深忆病人 2020-12-13 00:57

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 be

3条回答
  •  孤街浪徒
    2020-12-13 01:34

    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

提交回复
热议问题