[英]UILabel not working with NSAttributedString + NSParagraphStyle for Right-To-Left
I'm having problems with UILabels that use NSAttributedStrings
with NSParagraphStyle
when I use UISemanticContentAttributeForceRightToLeft
. 我在使用
NSParagraphStyle
时将NSAttributedStrings
与NSParagraphStyle
使用的UISemanticContentAttributeForceRightToLeft
。
I have a demo app with just 2 labels on the UI. 我有一个演示应用程序,UI上只有两个标签。
They have leading and trailing constraints. 它们具有前导和尾随约束。
I'm making the app Right to Left in the AppDelegate with 我在AppDelegate中使应用程序从右到左
UIView.appearance().semanticContentAttribute = .forceRightToLeft
And I'm just configuring the labels with the following code 我只是用以下代码配置标签
let labelAText = "Foo"
let mutAttrString = NSMutableAttributedString(string: labelAText)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .natural
paragraphStyle.baseWritingDirection = .natural
mutAttrString.setAttributes([NSAttributedStringKey.paragraphStyle: paragraphStyle.copy()], range: NSMakeRange(0, labelAText.count))
labelA.attributedText = mutAttrString
labelB.text = "Bar"
Which results in this: 结果是:
[ [
The labelB (Bar)
is working as expected, but the labelA (Foo)
isn't. labelB (Bar)
正常工作,但labelA (Foo)
无效。 And if I just remove the NSParagraphStyle
it starts working. 如果我只是删除
NSParagraphStyle
它将开始工作。 Mind that I'm not even changing anything on it, but just by using it it's messing up with the alignment. 请注意,我什至没有更改任何内容,只是通过使用它就弄乱了对齐方式。
Any thoughts on what might be causing this? 有什么想法可能导致这种情况?
NSMutableParagraphStyle has a property baseWritingDirection (enum NSWritingDirection ). NSMutableParagraphStyle具有属性baseWritingDirection (枚举NSWritingDirection )。 Set it to rightToLeft
将其设置为rightToLeft
Try this 尝试这个
paragraphStyle.baseWritingDirection = NSWritingDirection.rightToLeft
Edit :(May this help you) 编辑 :(可能对您有帮助)
If the value of property (baseWritingDirection) is NSWritingDirectionNaturalDirection, the receiver resolves the writing direction to either NSWritingDirectionLeftToRight or NSWritingDirectionRightToLeft, depending on the direction for the user's language preference setting.
如果属性(baseWritingDirection)的值为NSWritingDirectionNaturalDirection,则接收者将根据用户语言首选项设置的方向将写入方向解析为NSWritingDirectionLeftToRight或NSWritingDirectionRightToLeft。
class func defaultWritingDirection(forLanguage languageName: String?) -> NSWritingDirection
languageName : The language specified in ISO language region format. languageName :以ISO语言区域格式指定的语言。 Can be nil to return a default writing direction derived from the user's defaults database.
可以为nil以返回从用户的默认数据库得出的默认书写方向。
Solution using languageName = "ar" 使用languageName =“ ar”的解决方案
@IBOutlet weak var labelA: UILabel!
let labelAText = "Foo"
let mutAttrString = NSMutableAttributedString(string: "Foo")
let paragraphStyle = NSMutableParagraphStyle()
//paragraphStyle.alignment = .natural
paragraphStyle.baseWritingDirection = NSParagraphStyle.defaultWritingDirection(forLanguage: "ar")
mutAttrString.setAttributes([NSAttributedStringKey.paragraphStyle: paragraphStyle], range: NSMakeRange(0, labelAText.count))
labelA.attributedText = mutAttrString
Here is list of languages: Codes for the Representation of Names of Languages 这是语言列表:语言名称表示的代码
Note: Use ISO 639-1
code for language. 注意:语言请使用
ISO 639-1
代码。 Direction of label text depends upon natural direction of language. 标签文本的方向取决于语言的自然方向。
Here is result: 结果如下:
This Apple document will help you - Supporting Right-to-Left Languages 此Apple文档将为您提供帮助- 支持从右到左的语言
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.