'Change the size of UILabel or the font to fit in Accented Character

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?



Solution 1:[1]

The answer was to do

self.textLabel.numberOfLines = 2;

And then doing :

[self.textLabel sizeToFit];

Solution 2:[2]

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)
    }
} 
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gran_profaci
Solution 2 Noman Haroon