Question

I have a UILabel with Text inside it called :

self.textLabel; 

Now, I noticed that whenever I added an accented letter like Ä Ö Ü, then I get an effect like :

enter image description here

Notice how the top dots on the accented letters are cut off. I want to get the text to fit the label, however the self.textLabel.numberOfLines = 2 constraint which ensures that I have 2 lines and the text after the sentence goes beyond the width is maintained. Essentially, I want a label like :

enter image description here

With the dots maintained. Now, I have tried :

[self.frame sizeToFit] which does not work because it wraps around the entire text. As I said, cut the text out after 2 lines.

Using .bounds and CGRectMake to create new frames and then assign their new height to the current frame, which doesn't work either. Check out https://stackoverflow.com/questions/21948714/adjust-size-of-uilabel-to-fit-height-of-text for more information. Can someone please help me out with this?

Was it helpful?

Solution 2

The answer was to do

self.textLabel.numberOfLines = 2;

And then doing :

[self.textLabel sizeToFit];

OTHER TIPS

Adding top padding to label fixed the issue for me.

@IBDesignable class PaddingLabel: UILabel {

@IBInspectable var topInset: CGFloat = 5.0
@IBInspectable var bottomInset: CGFloat = 0
@IBInspectable var leftInset: CGFloat = 0
@IBInspectable var rightInset: CGFloat = 0

override func drawText(in rect: CGRect) {
    let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
    super.drawText(in: rect.inset(by: insets))
}

override var intrinsicContentSize: CGSize {
    let size = super.intrinsicContentSize
    return CGSize(width: size.width + leftInset + rightInset,
                  height: size.height + topInset + bottomInset)
}   

override var bounds: CGRect {
    didSet {
        // ensures this works within stack views if multi-line
        preferredMaxLayoutWidth = bounds.width - (leftInset + rightInset)
    }
} 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top