Search code examples
iosxcodeuilabelinterface-builder

Xcode UILabel bug? Line spacing cropping text with a UILabel


I have some designs I'm following for an iOS project. The font used is Avenir with relatively tight line spacing.

Some of these labels will have dynamic text, so I can't just make the label's size larger since the size should be determined by the content.

By default line spacing for a UILabel ends up pretty large.

Default Line Spacing

If I adjust the Line Height Multiple or the Max Height, the text along the top ends up cropped. Height Multiple


enter image description here

It should behave like this (Affinity Designer)... enter image description here

Is there a way to handle this?

Thanks for your help!


Solution

  • This works for me. By adding

    minimumLineHeight

    let string = NSMutableAttributedString(string: venue.name)
    let style = NSMutableParagraphStyle()
    style.lineHeightMultiple = 0.68
    style.minimumLineHeight = nameLabel.font.lineHeight
    string.addAttribute(NSAttributedString.Key.paragraphStyle,
                        value: style,
                        range: NSMakeRange(0, venue.name.count))
    nameLabel.attributedText = string