I have a label which has few lines of text and I want to increase the spacing between the lines. There are similar questions asked by others but the solutions don't solve my problems. Also my label may or may not contain paragraphs. I am new to Swift
. Is there a solution using storyboard? Or only through NSAttributedString
its possible?
To change the spacing between lines of text, you will have to subclass UILabel and roll your own drawTextInRect, or create multiple labels." This is a really old answer, and other have already addded the new and better way to handle this.. Please see the up to date answers provided below.
If you have created an UILabel programmatically, replace the UILabel class with the PaddingLabel and add the padding: // Init Label let label = PaddingLabel() label. backgroundColor = . black label.
Line spacing is the amount of space between lines of text within a paragraph, the property set by “line-height” in HTML code. Line spacing is expressed in HTML as a number value or factor of the font size, such as 1.5× or 150%. As an example: 1.5× line height on size 12 text is 18 (by math 12 × 1.5).
Programatically add LineSpacing to your UILabel
using following snippet.
Earlier Swift version
let attributedString = NSMutableAttributedString(string: "Your text") // *** Create instance of `NSMutableParagraphStyle` let paragraphStyle = NSMutableParagraphStyle() // *** set LineSpacing property in points *** paragraphStyle.lineSpacing = 2 // Whatever line spacing you want in points // *** Apply attribute to string *** attributedString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attributedString.length)) // *** Set Attributed String to your label *** label.attributedText = attributedString
Swift 4.0
let attributedString = NSMutableAttributedString(string: "Your text") // *** Create instance of `NSMutableParagraphStyle` let paragraphStyle = NSMutableParagraphStyle() // *** set LineSpacing property in points *** paragraphStyle.lineSpacing = 2 // Whatever line spacing you want in points // *** Apply attribute to string *** attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length)) // *** Set Attributed String to your label *** label.attributedText = attributedString
Swift 4.2
let attributedString = NSMutableAttributedString(string: "Your text") // *** Create instance of `NSMutableParagraphStyle` let paragraphStyle = NSMutableParagraphStyle() // *** set LineSpacing property in points *** paragraphStyle.lineSpacing = 2 // Whatever line spacing you want in points // *** Apply attribute to string *** attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length)) // *** Set Attributed String to your label *** label.attributedText = attributedString
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With