Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add indents to Multiple lines of UILabel?

While I found how to add indentations to first line (FirstLineHeadIndent) and to the rest of lines (HeadIndent), I cannot find how to add indents to only first two/three lines in order to achieve something like this: long live MS Paint!

PS: This is not a duplicate, because I'm not asking how to indent only first line, as one user suggested.

like image 275
nicks Avatar asked Dec 13 '25 05:12

nicks


1 Answers

You need to set your UILabel text as Attributed string in storyboard.

Then you can edit the indentation of each line, and you can also paste any text you've created with text editor and it will keep its indentation as well as other attributes.

You can of course manipulate these attributes programmatically, here is an example:

@IBOutlet weak var label: UILabel!
 let text = "\tfirst line\n \tsecond line\nthird line\nforth line"

 let paragraphStyle = NSMutableParagraphStyle()
 paragraphStyle.tabStops = [NSTextTab(textAlignment: NSTextAlignment.left, location: 15, options: [:])]
 paragraphStyle.headIndent = 10

 label.attributedText = NSAttributedString(string: text, attributes: [NSParagraphStyleAttributeName: paragraphStyle])

Here is an example of how to configure it:

enter image description here

Here is how to configure indentation:

enter image description here

Here is the example on the simulator:

enter image description here

like image 136
inspector_60 Avatar answered Dec 15 '25 17:12

inspector_60



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!