iOS RTL - 在RTL字符串中不正确显示英语

9

iOS应用程序,我们要显示来自服务器的新闻。 使用UIlabel

  • 当句子仅为单一语言且不考虑布局时,一切都很完美(我们会针对不同语言,包括阿拉伯语和希伯来语,切换布局从RTL到LTR)
  • 当LTR语言中有RTL单词时,它们会破坏句子结构(请参见图片,BTN必须在行首,但它跳到了行尾) English words inside RTL language 有什么解决方法吗?提前感谢:)
5个回答

16

苹果正在使用“Unicode双向算法”来展示文本。如果字符串中的第一个字符是LTR,则该算法将其余字符串的呈现视为LTR。如果您提前知道字符串的语言是RTL,则可以使用Unicode\u200F和\u202c来强制RTL对齐。

Objective-C

[NSString stringWithFormat:@"\u200F%@\u202c", @"your string with RTL content"]

[NSString stringWithFormat:@"\u200E%@\u202c", @"your string with LTR content"]

Swift

String(format: "\u200F%@\u202c", "your string with RTL content")

String(format: "\u200E%@\u202c", "your string with LTR content")

3
这对我似乎有效,关于此问题的相关Apple文档可以在此找到: https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPInternational/SupportingRight-To-LeftLanguages/SupportingRight-To-LeftLanguages.html - jonhurlock

4

这是@Pichirichi提供的Swift 5.2解决方案:

"\u{200F}\("your string with RTL content")\u{202c}"

"u200E\("your string with LTR content")\u{202c}"

4

Swift 5:

extension String {
    
    func forceUnicodeRTL() -> String {
        return "\u{200F}\(self)\u{200E}"
    }
}

2
  "arabic text \u200E english text \u200F arabic text \u200E english text"

解决了问题。

它对我没用。我的字符串是这样的 self.testLabel.text = @"رقم HELLO سجل"; - Adeel Ur Rehman

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接