设计同学总是会有层出不穷的想法,本来一段简单的文本,系统的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];

7120

被折叠的 条评论
为什么被折叠?



