Xcode: Is there a way to change line spacing (UI Label) in an interface builder?

I have several UILabels with several lines of text, but the distance between the lines is greater than I would prefer. Is there any way to change this?

+6
source share
3 answers

With iOS 6, Apple added NSAttributedString to UIKit , which allows you to use NSParagraphStyle to change line spacing.

To actually change it from the NIB, see souvickcse answer.

+2
source

hi is a late answer, but it can help at some height of one line to change the text change from simple to attribute enter image description here

+37
source

Since I hate using attributed text in an interface constructor (I always encounter IB errors), here is an extension that allows you to set the line height that is a multiple of UILabel directly in the interface constructor

 extension UILabel { @IBInspectable var lineHeightMultiple: CGFloat { set{ //get our existing style or make a new one let paragraphStyle: NSMutableParagraphStyle if let existingStyle = attributedText?.attribute(NSAttributedString.Key.paragraphStyle, at: 0, effectiveRange: .none) as? NSParagraphStyle, let mutableCopy = existingStyle.mutableCopy() as? NSMutableParagraphStyle { paragraphStyle = mutableCopy } else { paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = 1.0 paragraphStyle.alignment = self.textAlignment } paragraphStyle.lineHeightMultiple = newValue //set our text from existing text let attrString = NSMutableAttributedString() if let text = self.text { attrString.append( NSMutableAttributedString(string: text)) attrString.addAttribute(NSAttributedString.Key.font, value: self.font, range: NSMakeRange(0, attrString.length)) } else if let attributedText = self.attributedText { attrString.append( attributedText) } //add our attributes and set the new text attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length)) self.attributedText = attrString } get { if let paragraphStyle = attributedText?.attribute(NSAttributedString.Key.paragraphStyle, at: 0, effectiveRange: .none) as? NSParagraphStyle { return paragraphStyle.lineHeightMultiple } return 0 } } 
0
source

Source: https://habr.com/ru/post/923685/


All Articles