UILabel

作者: 墓园派对 | 来源:发表于2018-09-07 10:19 被阅读0次

1.设置文字距离边框的距离

.h

@interface BKHouseLiveCommentLabel : UILabel
@property (nonatomic) UIEdgeInsets contentInsets;
@end

.m

@implementation BKHouseLiveCommentLabel
- (instancetype)init {
    self = [super init];
    if (self) {
        _contentInsets = UIEdgeInsetsMake(5, 10, 5, 10);
    }
    return self;
}

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
    CGRect rect = [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, _contentInsets) limitedToNumberOfLines:numberOfLines];
    rect.origin.x -= _contentInsets.left;
    rect.origin.y -= _contentInsets.top;
    rect.size.width += _contentInsets.left + _contentInsets.right;
    rect.size.height += _contentInsets.top + _contentInsets.bottom;
    return rect;
}

- (void)drawTextInRect:(CGRect)rect {
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, _contentInsets)];
}
@end

2.添加背景图片

UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tagBackground"]];
self.backgroundColor = color;

3.没有离屏渲染的圆角

//label.backgroundColor设置的是contents的背景色,不是layer的背景色
//cornerRadius:影响layer,不影响contents
label.cornerRadius = 10;
label.layer.backgroundColor = [UIColor redColor].CGColor;

相关文章

网友评论

      本文标题:UILabel

      本文链接:https://www.haomeiwen.com/subject/dziqgftx.html