How can I make UILabel be wider than it should be by 5 points?

I have a UILabel, it has a background color like green. But he very much wraps around the text. I need a green background that will be slightly wider than the text, about 5 points. How can I achieve this?

More details

A shortcut is created in the storyboard and has horizontal restrictions. You can ignore the restrictions, as I assume, because I processed it for any width.

Recommended Requirement

I think I need a method that I can override in my UILabel subclass to increase its width by 5 points. Does anyone know which method I prefer?

+4
source share
4 answers

, ! , -

UILabel intrinsicContentSize sizeThatFits, .

, - :

- (CGSize) intrinsicContentSize
{
    return [self addHorizontalPadding:[super intrinsicContentSize]];
}

- (CGSize)sizeThatFits:(CGSize)size
{
    return [self addHorizontalPadding:[super intrinsicContentSize]];
}

- (CGSize)addHorizontalPadding:(CGSize)size
{
    if (size.width > 0)
        return CGSizeMake(size.width + (2*kSomeHorizontalPaddingValue), size.height);
    else
        return size;
}

, , , , , .

+1

UILabel -

- (void)drawTextInRect:(CGRect)rect {
    UIEdgeInsets insets = {0, 5, 0, 5};
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}

, !

+3

, sizeToFit, , 10 .

, .. ...:)

+1

,

CGFloat width =  [label.text sizeWithFont:[UIFont systemFontOfSize:22 ]].width;

label.frame = CGRectMake(point.x, point.y, width+5,height);
0

All Articles