UILabel控制内容边距

设计同学总是会有层出不穷的想法,本来一段简单的文本,系统的UILabel就能实现的,非要加个背景,加个边距,加一些奇奇怪怪的东西,自家的设计同学,只能宠着了,实现吧。以下是自定义UILabel实现内容边距的简单实现,供参考:

//EdgeInsetsLabel.h



@interface EdgeInsetsLabel : UILabel

@property (nonatomic, assign) UIEdgeInsets textInsets; // 控制字体与控件边界的间隙

@end
//EdgeInsetsLabel.m

#import "PSEdgeInsetsLabel.h"



@implementation EdgeInsetsLabel

- (instancetype)init {

    if (self = [super init]) {

        _textInsets = UIEdgeInsetsZero;

    }

    return self;

}



- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {

        _textInsets = UIEdgeInsetsZero;

    }

    return self;

}



- (void)drawTextInRect:(CGRect)rect {

    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, _textInsets)];

}



@end
​
//使用demo



    _signLabel = [[EdgeInsetsLabel alloc] init];

    _signLabel.textColor = [UIColor blackColor];

    _signLabel.font = [UIFont systemFontOfSize:14];

    _signLabel.numberOfLines = 0;

    _signLabel.backgroundColor = PFSameRGBColor(248);

    _signLabel.textInsets = UIEdgeInsetsMake(0,12,0, 12);

    [self.contentView addSubview:self.signLabel];

​

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值