指定字符串的语言方向并正确显示
我有一个应用程序读取一些字符串并在一些 UILabels 中显示它们,但这些字符串不是来自一种语言,因此如果字符串来自 RTL 语言,我需要能够将标签的对齐方式设置为右对齐,将标签对齐设置为左对齐否则。
那么是否有直接的方法来实现这一点,如果没有,从字符串获取语言方向的最佳方法是什么。 我正在考虑检查字符串的前 X 个字符,看看 LTR 和 RTL 字符的较大百分比是多少,以根据它设置标签的对齐方式。这是一个好的解决方案吗?如果是的话,我在哪里可以找到一个表来显示属于 LTR 字符的 unicode 和 RTL 字符的 unicode。
I have an application that reads some strings and displays them in some UILabels, but these strings are not from one language so I need to be able to set the alignment of the label to right if the string is from a RTL language, and to left otherwise.
So is there a direct way to accomplish that, and if not what's the best way to get the language's direction from a string.
I'm thinking of checking the first X characters of the string and see what is the larger percentage of the LTR and RTL characters to set the alignment of the label according to it. Is this a good solution and if so where could I find a table that shows what are the unicodes that belongs to LTR characters and what those for RTL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在具有适当 RTL 支持的程序中,您不需要为任何简单文本(例如单词和空格)设置方向。程序应应用字符代码标准中定义的字符本身的属性。
如果要呈现为一个单元的一段文本包含标点符号字符和/或 LTR 和 RTL 字母的混合,则应使用控制字符或在更高的协议级别设置方向。我认为你不应该对此进行启发式处理;相反,总体方向应该是文本项的属性,由信息提供者(例如,作者、翻译者)决定并与文本一起存储。在软件本地化中,一组文本的本地化值通常具有一种语言,并且总体方向应作为数据集的一部分存储或从语言推断。
请注意,书写方向还应影响整体布局方向(例如,对于 RTL 语言,表格列应从右向左排列)和文本对齐方式。
In programs with appropriate RTL support, you do not need to set the direction for any simple text like just words and spaces. Programs are expected to apply the properties of the characters themselves, as defined in character code standards.
If a piece of text to be rendered as a unit contains punctuation characters and/or a mix of LTR and RTL letters, the direction should be set either with control characters or at a higher protocol level. I don’t think you should do heuristics on this; rather, the overall direction should be a property of the text item, decided by the information provider (e.g., author, translator) and stored along with the text. In software localization, a set of localized values for texts normally has one language and the overall direction should be stored as part of the data set or inferred from the language.
Note that writing direction should also affect the overall layout direction (e.g., table columns should run from right to left for RTL languages) and the text alignment.