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.rightsize.height +=insets.top + insets.bottomreturn size}override func drawTextInRect(rect: CGRect) {super.drawTextInRect(UIEdgeInsetsInsetRect(rect, insets))}

However, in this particular case the insets are zero.

  • 2
    try to increase UILabel height because the height of label is less than the font size or decrease font size.– Chirag D jinjuwadiyaMar 23 '16 at 11:03
  • As you can see, the text height is lower than label's height. Also, if I use regular label instead of this subclass, the text is not cut off.– mag_zbcMar 23 '16 at 11:08
  • @mag_zbc which custom font are you using.– Nimit ParekhMar 23 '16 at 11:30

Turns out the problem was with

self.lineBreakMode=.ByClipping

changing it to

self.lineBreakMode=.ByCharWrapping

Solved the problem

    Happened for me when providing topAnchor and centerYAnchor for label at the same time.Leaving just one anchor fixed the problem.

      Other answers didn't help me, but what did was constraining the height of the label to whatever height it needed, like so:

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

      Also, sizeThatFits(_:) will return a 0 by 0 size if your label's text field is nil or equal to ""

        Your Answer

         
        discard

        By posting your answer, you agree to the privacy policy and terms of service.

        Not the answer you're looking for? Browse other questions tagged or ask your own question.