UILabel

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;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,037评论 3 119
  • 就在刚刚,合上这本书之后,我走到厨房,拿起一个木瓜,慢慢洗好、切开、清理里面的子、去皮,然后一点一点切成小块,摆好...
    云朵的书房阅读 3,349评论 1 1
  • [TOC] 前言 上一篇,扫码的基本功能已经实现,不过还存在一些问题 扫码界面不是我们常见的二维码扫描界面 方法调...
    好多个胖子阅读 17,518评论 4 114
  • 昨天刮了大风 今天下了大雨 明天老天爷要怎样 我不关心 我只关心 你要怎样
    北辰浪儿阅读 715评论 0 1
  • —1— 昨天有个不算太熟的人跟我见面,没过多久就开始炫耀她的男朋友长得帅,人又上进,现在在哪里买了车买了房,昨天还...
    女生动漫阅读 3,229评论 0 1