UILabel subclass - the text is trimmed at the bottom, despite the fact that the label is of the correct height

I have a problem with a subclass of UILabel cutting off the text below. The label has the correct height to fit the text, there is some space at the bottom, but the text is still cropped.

Label

Red bars are added to the label layer.

I will subclass the label to add insertion inserts.

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 inserts are zero.

+5
source share
3 answers

Turns out the problem was

 self.lineBreakMode = .ByClipping 

changing it to

 self.lineBreakMode = .ByCharWrapping 

Problem solved

+7
source

It happened to me while providing the topAnchor and centerYAnchor labels. Fix a problem with a single anchor.

+1
source

Other answers did not help me, but what limited the height of the mark to any height it needed:

 let unconstrainedSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude) label.heightAnchor.constraint(equalToConstant: label.sizeThatFits(unconstrainedSize).height).isActive = true 

In addition, sizeThatFits(_:) will return a size of 0 by 0 if the label field text is nil or equal to ""

0
source

Source: https://habr.com/ru/post/1245621/


All Articles