xcode 在Xcode中,如何显示合并英语+阿拉伯语并以阿拉伯语开头的文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12366330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Xcode, how to display text merging English+Arabic and beginning with Arabic?
提问by user1300503
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 ???"?
我想将标签设置为字符串:“??? 刚买了:伪装套件。” 但是当我运行测试时,标签显示“.just buy: 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?
有人知道如何处理这个问题吗?
回答by Rob Napier
First, read Cal Henderson's excellent "Understanding Bidirectional (BIDI) Text in Unicode.".
首先,阅读 Cal Henderson 出色的“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).
信不信由你,UILabel
就是按照你的要求进行布局。您提供了一个从右到左的字符串(以阿拉伯语开头)。它开始从右到左显示。然后您嵌入了一个从左到右的英文字符串,它从左到右排列。然后它看到句点,并将它从右到左排列,因为这是一个阿拉伯字符串,恰好在其中包含一些英语(UILabel
尽人皆知)。
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.
您想要的是一个从左到右嵌入阿拉伯语的字符串。这意味着您必须以从左到右的字符开始字符串。两种选择:在开头添加一些英文,或使用零宽度从左到右标记(U+200E,LRM)将字符串的开头锚定到 LTR 模式。
Objective-C:
目标-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.
关于 U+200E 的好消息是,您可以在显示之前安全地将其添加到每个 LTR 字符串中。您甚至可以安全地将它放在 LTR 语言的本地化字符串的开头。如果它是多余的,它不会伤害任何东西。
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.
需要注意的其他几件事:永远不要用 ??? 测试这些东西,总是用 ??? 测试 (就像每个好学生一样:D)或者更好的是像 ???。否则你无法分辨阿拉伯语何时向后排列。我喜欢 ???因为它向后看起来大不相同。
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.
另外,在测试时,请注意 Xcode 不知道如何布局阿拉伯语。因此,如果您在代码中编写任何静态字符串,它们将在编辑器中向后显示,但它们将在 UI 中正确显示。让我发狂。
回答by guru_meditator
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.
由于 iOS 10(和 macOS 10.12)String localizedStringWithFormat
在占位符周围插入了 Unicode 隔离。这是使用混合语言方向格式化字符串的高级方法,无需手动插入方向标记。
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:
要查看 localizedStringWithFormat 正在做什么:
let scalars = String.localizedStringWithFormat("%@ just bought: Disguise Kit.", "???")
.unicodeScalars.map { "U+\(String(format: "%04X", ##代码##.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
其中 U+2068 是第一强隔离,U+2069 是 POP 方向隔离。您可以在以下位置阅读有关隔离的更多信息: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
此功能是在WWDC 2016 session 232 What's New in International User Interfaces 中引入的
回答by Andrey
Actually, \u{200E} (or any) symbol can be inserted into the string literal.
实际上,\u{200E}(或任何)符号可以插入到字符串文字中。
- Add "Unicode Hex Input" (System Preferences -> Keyboard -> Input Sources -> + -> Unicode Hex Input).
- In the text editor move the cursor to the place where you want to insert a symbol.
- Switch input source to the "Unicode Hex Input".
- Hold the Option key and type 200E.
- The symbol should be inserted and the string should look like you expect.
- 添加“Unicode 十六进制输入”(系统偏好设置 -> 键盘 -> 输入源 -> + -> Unicode 十六进制输入)。
- 在文本编辑器中,将光标移动到要插入符号的位置。
- 将输入源切换到“Unicode Hex Input”。
- 按住 Option 键并键入 200E。
- 应该插入符号并且字符串应该看起来像您期望的那样。