Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
602 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...