苹果正在使用“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")
这是@Pichirichi提供的Swift 5.2解决方案:
"\u{200F}\("your string with RTL content")\u{202c}"
"u200E\("your string with LTR content")\u{202c}"
Swift 5:
extension String {
func forceUnicodeRTL() -> String {
return "\u{200F}\(self)\u{200E}"
}
}
"arabic text \u200E english text \u200F arabic text \u200E english text"
self.testLabel.text = @"رقم HELLO سجل";
- Adeel Ur Rehman这里的一些信息是有帮助的:
[[self label] setTextAlignment:NSTextAlignmentNatural];
AutoLayout + RTL + UILabel文本对齐
但有时它仍然不能按预期工作