作为
UIView
的子类,
UILabel
有一个名为
semanticContentAttribute
的变量,您可以将其设置为
.foreRightToLeft
,也可以通过属性检查器中的语义弹出菜单从nib inspector进行设置。
此外,您可以查询effectiveUserInterfaceLayoutDirection
属性来调试其状态。
请参阅此处了解更多信息。
现在,如果您需要在一个标签中使用两种对齐方式,这将是棘手的。要么将两个标签组合到一个UIView
容器中,要么查看是否可以为NSMutableAttributedString
的一部分设置这些值,然后将其提供给UILabel
。
您尝试设置的textAlignment
属性将为段落对齐提供与MS-Word相同的效果,但不会翻转语言的阅读方向。
祝编码愉快!
编辑:这是使用属性字符串建议的示例,尽管将阿拉伯语设置更改为从右到左,但它会将其放置在字符串底部...也许需要以不同的方式组合标志?
override func viewDidLoad() {
super.viewDidLoad()
let label = UILabel()
let myMutableString = NSMutableAttributedString()
let multipleAttributes: [String : AnyObject] = [
NSForegroundColorAttributeName: UIColor.orangeColor(),
NSBackgroundColorAttributeName: UIColor.blueColor(),
NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleDouble.rawValue,
NSWritingDirectionAttributeName : [NSWritingDirection.LeftToRight.rawValue ]
]
let myAttrString = NSAttributedString(string: "عبد الله", attributes: multipleAttributes)
myMutableString.appendAttributedString(myAttrString)
let someText = NSAttributedString(string: " finds ", attributes: nil)
myMutableString.appendAttributedString(someText)
let multipleAttributes2: [String : AnyObject] = [
NSForegroundColorAttributeName: UIColor.blueColor(),
NSBackgroundColorAttributeName: UIColor.yellowColor(),
NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleDouble.rawValue,
NSWritingDirectionAttributeName : [NSWritingDirection.LeftToRight.rawValue | NSTextWritingDirection.Embedding.rawValue]
]
let myAttrString2 = NSAttributedString(string: "restaurant", attributes: multipleAttributes2)
myMutableString.appendAttributedString(myAttrString2)
label.attributedText = myMutableString
self.view.addSubview(label)
label.sizeToFit()
label.center = self.view.center
}
\u{200E}
开头的字符序列)末尾添加“弹出方向格式化”字符(即\u{200C}
)。您可以在此处阅读有关方向格式化字符的更多信息:http://www.unicode.org/reports/tr9/。 - Adil Hussain