我有一个已归因于文字的标签。我想要实现像这个图像的东西。 sample label
子类别和价格文本应分别左右对齐并在一行中。 子类别文本之前有一些空格。 请提供此类归属文本的任何示例代码。
答案 0 :(得分:1)
我认为您需要使用段落样式并添加头部缩进。
let style = NSMutableParagraphStyle()
style.headIndent = 100 // your value here
然后添加段落样式作为属性。
[NSParagraphStyleAttributeName : style]
Swift 4& 5 强>
[NSAttributedString.Key.paragraphStyle : style]
答案 1 :(得分:0)
let myLabelWidth = 300
let myLabelHeight = 50
let paragraph = NSMutableParagraphStyle()
paragraph.tabStops = [
NSTextTab(textAlignment: .right, location: CGFloat(myLabelWidth), options: [:]),
]
let attributed = NSAttributedString(
string: "Subcategory 1\t$30",
attributes: [NSAttributedStringKey.paragraphStyle: paragraph]
)
// Change 'x' value to "some" space before the text
let myLabel = UILabel(frame: CGRect(x: 0,
y: 0,
width: myLabelWidth,
height: myLabelHeight))
// Just added to see boundaries of the view that contains the text
myLabel.backgroundColor = UIColor.lightGray
myLabel.attributedText = attributed