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 } }
source share