Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent UILabel from clipping it's text when the font size is larger than the label height

I have a circumstance in my app whereby a label may be given a font size greater than it's height. This is to do with some rather complex architecture and layout code. Normally I would increase the label's height to accommodate the larger font but that is profoundly difficult to do in my scenario and I would rather avoid it.

The next logical thing is to turn clipsToBounds off, to allow the text sublayer to overflow the bounds of the label. Unfortunately this seems to have no effect in this case as the text is still clipped.

Am I missing something?

like image 354
Jacob King Avatar asked Oct 18 '17 08:10

Jacob King


1 Answers

Looking at the documentation for UILabel:

https://developer.apple.com/documentation/uikit/uilabel/1620545-textrect

I think you need to override the method textRect(forBounds:limitedToNumberOfLines:) by explicitly increasing the rectangle returned by this method to the containing size of the label’s string rather than the bounds of the label.

(This solution does of course require you to subclass.)

Hope that helps.

like image 142
Marcus Avatar answered Oct 03 '22 23:10

Marcus