Reputation: 51
I have a label that has attributed text. I want to achieve something look like this image. sample label
Subcategory and price text should be left and right alignment respectively and in a single line. There is some space before Subcategory text. Please provide any sample code for this kind of attributed text.
Upvotes: 1
Views: 2584
Reputation: 1413
I think you need to use a paragraph style and add a head indent.
let style = NSMutableParagraphStyle()
style.headIndent = 100 // your value here
Then add the paragraph style as the attribute.
[NSParagraphStyleAttributeName : style]
Swift 4 & 5
[NSAttributedString.Key.paragraphStyle : style]
Upvotes: 2
Reputation: 2754
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
Upvotes: 0