I have a UILabel with attributed text that includes a specific lineSpacing or lineHeightMultiple.
let titleParagraphStyle = NSMutableParagraphStyle() //titleParagraphStyle.lineSpacing = lineSpacing titleParagraphStyle.lineHeightMultiple = 1.5 titleParagraphStyle.alignment = .left let titleAttributes = [NSForegroundColorAttributeName: color, NSFontAttributeName: UIFont(name: fontName, size: fontSize)!, NSParagraphStyleAttributeName : titleParagraphStyle] as [String : Any] let titleAttrString = NSAttributedString(string:string, attributes: titleAttributes) var label = BetterUILabel() label.numberOfLines = 0 label.attributedText = titleAttrString label.setNeedsLayout() //I'm trying everything label.layoutIfNeeded() label.sizeToFit()
BetterUILabel is a subclass of UILabel with fixes for perferredMaxLayoutWidth
class BetterUILabel: UILabel { override public func layoutSubviews(){ super.layoutSubviews() self.preferredMaxLayoutWidth = self.bounds.width super.layoutSubviews() } override public var bounds: CGRect { didSet { self.preferredMaxLayoutWidth = self.bounds.width } } override public var frame: CGRect { didSet { self.preferredMaxLayoutWidth = self.frame.width } } }
label is a subtype of uiView, which is an ordered view of a vertical UIStackView within a UIScrollView .
The shortcut is attached to the top, left and right screens. He relies on his own content size to manage it. If I remove any lineSpacing or lineHeightMultiple from the style or use plain unattached text, it will be beautiful. However, with these attributes, it sometimes disables the last line on devices 6+ and 7+.
I exaggerated the line spacing to show the problem.
With an orange background, UILabel runs on 6: 
At 6+, with a green background, UILabel seems to have a vertical space at the top that causes text to truncate: 
What causes this. Is there a problem with this problem?
source share