Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Increase Line spacing in UILabel in Swift

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?

like image 769
Sneha Avatar asked Aug 26 '16 05:08

Sneha


People also ask

How do you increase line spacing in UILabel?

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.

How do I add padding to UILabel Swift?

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.

Is line spacing the same as line height?

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).


1 Answers

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 
like image 52
Dipen Panchasara Avatar answered Oct 01 '22 14:10

Dipen Panchasara