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;
网友评论