项目里有标签的样式要做,只展示一个标签,首先想到了用label来做,可是label没有设置edgeInsets的方法,那么久只能自定义了,废话不多说,直接上代码
- (instancetype)initWithFont:(UIFont *)textFont withTextColor:(UIColor *)textColor;
{
if (self = [super init]) {
self.layer.borderWidth = 1;
self.layer.borderColor = [UIColor blackColor].CGColor;
self.layer.cornerRadius = 4;
self.textAlignment = NSTextAlignmentCenter;
self.font = textFont;
self.textColor = textColor;
}
return self;
}
- (CGSize)intrinsicContentSize {
CGSize originalSize = [super intrinsicContentSize];
if (originalSize.width<0.9 || originalSize.height<0.9) {//当自身没有大小(text.length<1)
return originalSize;
}
CGSize size = CGSizeMake(originalSize.width + 2 * 10, originalSize.height + 2 * 5);
return size;
}
- (EdgeInsetsLabel *)titleLabel
{
if (!_titleLabel) {
_titleLabel = [[EdgeInsetsLabel alloc] initWithFont:[UIFont systemFontOfSize:12] withTextColor:[UIColor orangeColor]];
}
return _titleLabel;
}
[self.view addSubview:self.titleLabel];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view).offset(100);
make.left.mas_equalTo(self.view).offset(80);
}];
self.titleLabel.text = @"测试label内边距";
这样问题就解决了