How can I add padding to the intrinsic content size of UILabel?

前端 未结 4 576
南笙
南笙 2021-02-04 02:09

I\'m using autolayout on iOS7 and I have a problem like this:

I\'m putting a UILabel onto a UIView and I\'m arranging my autolayout constraints so that the label\'s cen

4条回答
  •  眼角桃花
    2021-02-04 02:35

    You can create a UILabel subclass and override intrinsicContent,

    -(CGSize)intrinsicContentSize {
        CGSize s = [super intrinsicContentSize];
        s = CGSizeMake(s.width + 20, s.height);
        return s;
    }
    

    This will add a padding of 20 points to the width. If you want your text in the middle, be sure to set the text alignment to center.

提交回复
热议问题