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

前端 未结 4 502
南笙
南笙 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:20

    If you're using auto layout, you can set the horizontal constraints and use an NSDictionary in the metrics parameter to set this dynamically.

    For instance, if you wanted to give a 10pt padding to the inner content of a UIButton, you could do something like the following:

    NSDictionary *padding = @{ @"padding" : @(button.intrinsicContentSize.width + 20) };
    NSArray *buttonHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[button(==padding)]|" options:0 metrics:padding views:NSDictionaryOfVariableBindings(button)];
    

提交回复
热议问题