【发布时间】:2015-08-11 15:00:26
【问题描述】:
【问题讨论】:
-
这更多地与字符属性的布局有关,所以我认为这不适合
NSAttributedString。 -
你应该考虑使用布局约束。
-
AutoLayout constrants 一定会帮助你
标签: ios objective-c nsattributedstring
【问题讨论】:
NSAttributedString。
标签: ios objective-c nsattributedstring
NSAttributedString 可以创建带有制表位的文本列。这类似于在具有相同限制的文字处理器中完成的方式。
let text = "Name\t: Johny\nGender\t: Male\nAge\t: 25\nFavourites\t: Reading, writing"
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.tabStops = [NSTextTab(textAlignment: NSTextAlignment.Left, location: 150, options: [:])]
paragraphStyle.headIndent = 150
label.attributedText = NSAttributedString(string: text, attributes: [NSParagraphStyleAttributeName: paragraphStyle])
tabStops 提供在每个制表符后继续文本的位置。在这里,我们在第一列之后的合理位置做了一个选项卡。
headIndent 告诉标签,被换行的文本需要缩进一个固定的量,所以它会换行到下一行。
这种方法的局限性在于:
headIndent 或将“:”拆分为 \t:\t 并设置第二个制表位。如果您不让文本换行,这不是问题。如果这些限制过于严格,您可以将标签重组为具有自动布局约束的多个标签的集合。
【讨论】:
在 Swift 4.2 或更高版本中
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.tabStops = [NSTextTab.init(textAlignment: .left, location: 150, options: [:])]
paragraphStyle.headIndent = 150
let attributedTitle = NSAttributedString(string: "Some Title", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14.0), NSAttributedString.Key.paragraphStyle: paragraphStyle])
【讨论】: