在Xcode中,如何显示合并英语+阿拉伯语并以阿拉伯语开头的文本? [英] In Xcode, how to display text merging English+Arabic and beginning with Arabic?

查看:39
本文介绍了在Xcode中,如何显示合并英语+阿拉伯语并以阿拉伯语开头的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将标签设置为字符串:خخخ 刚刚购买:伪装套件."但是当我运行测试时,标签显示.just buy: Disguise Kit خخخ"?

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?

推荐答案

首先阅读 Cal Henderson 的优秀了解 Unicode 中的双向 (BIDI) 文本.".

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

信不信由你,UILabel 正在按照您的要求进行布局.您提供了一个从右到左的字符串(以阿拉伯语开头).它开始从右到左显示.然后您嵌入了一个从左到右的英文字符串,它从左到右排列.然后它会看到句点,并将其从右到左排列,因为这是一个阿拉伯字符串,恰好在其中包含一些英语(正如 UILabel 所知道的那样).

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

您想要的是一个从左到右嵌入阿拉伯语的字符串.这意味着您必须以从左到右的字符开始字符串.两种选择:在开头添加一些英文,或者使用零宽度从左到右标记(U+200E,LRM)将字符串的开头锚定为LTR模式.

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.

目标 C:

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

斯威夫特:

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

关于 U+200E 的好消息是,您可以在显示之前安全地将其添加到每个 LTR 字符串中.您甚至可以安全地将它放在 LTR 语言的本地化字符串的开头.如果它是多余的,它不会伤害任何东西.

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.

另外几件事要注意:永远不要用 ككك 测试这些东西,总是用 كتب(就像每个好学生 :D)或更好的东西像 الو 测试.否则你无法分辨阿拉伯语何时向后排列.我喜欢 الو 因为它向后看起来完全不同.

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.

此外,在测试时,请注意 Xcode 不知道如何布局阿拉伯语.因此,如果您在代码中编写任何静态字符串,它们将在编辑器中向后显示,但它们将在 UI 中正确显示.快把我逼疯了.

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.

请务必参阅 guru_meditator 的答案,了解 iOS 10+/macOS 10.12 中添加的有用相关功能.

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

这篇关于在Xcode中,如何显示合并英语+阿拉伯语并以阿拉伯语开头的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆