Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel subclass - text cut off in bottom despite label being correct height

Tags:

ios

uilabel

swift

I have a problem with UILabel subclass cutting off text in the bottom. Label is of proper height to fit the text, there is some space left in the bottom, but the text is still being cut off.

The label

The red stripes are border added to label's layer.

I subclass the label to add edge insets.

override func sizeThatFits(size: CGSize) -> CGSize {
    var size = super.sizeThatFits(size)
    size.width += insets.left + insets.right
    size.height += insets.top + insets.bottom
    return size
}

override func drawTextInRect(rect: CGRect) {
    super.drawTextInRect(UIEdgeInsetsInsetRect(rect, insets))
}

However, in this particular case the insets are zero.

like image 771
mag_zbc Avatar asked Mar 23 '16 11:03

mag_zbc


3 Answers

Turns out the problem was with

self.lineBreakMode = .ByClipping

changing it to

self.lineBreakMode = .ByCharWrapping

Solved the problem

like image 195
mag_zbc Avatar answered Nov 18 '22 01:11

mag_zbc


I was facing the same issue with Helvetica Neue Condensed Bold font. Changing label's Baseline property from Align Baselines to Align Centers did the trick for me. You can change this easily in storyboard by selecting your label.

like image 12
Mohit Singh Avatar answered Nov 18 '22 00:11

Mohit Singh


My problem was that the label's (vertical) content compression resistance priority was not high enough; setting it to required (1000) fixed it.

It looks like the other non-OP answers may be some sort of workaround for this same underlying issue.

like image 11
juliand665 Avatar answered Nov 18 '22 00:11

juliand665