UILabel自定义文字,内增高,扩大padding
按钮button不会有这个问题,边框和文字之间本来就有间隙
增加Label的宽高
[self.promotionIcon mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self);
make.left.equalTo(self).offset(12.5);
// make.size.mas_equalTo(CGSizeMake(30, 13));
}];
- (void)setPromotionLabelText:(NSString *)text {
self.promotionIcon.text = text;
[self.promotionIcon sizeToFit];
CGRect bounds = self.promotionIcon.bounds;
[self.promotionIcon mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(bounds.size.width+5);
make.height.mas_equalTo(bounds.size.height+4);
}];
}
- 初始化设置位置
- 设置文字时sizeToFit,拿到bounds后,增加宽度
- iOS开发笔记 | 自定义具有内边距的label
Label文字滚动
1、删除线
- (NSAttributedString *)makeTheAmountNumber:(NSString *)amountStr {
NSString *lastStr = [NSString stringWithFormat:@"¥%@元", amountStr];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:lastStr];
[attributedString addAttributes:@{
NSForegroundColorAttributeName : [UIColor darkGrayColor],
NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle|NSUnderlinePatternSolid),
NSStrikethroughColorAttributeName:[UIColor orangeColor]
}
range:NSMakeRange(0, amountStr.length+1)];
return attributedString.copy;
}
self.nameLabel.attributedText = [self makeTheAmountNumber:@"88.88"];
------------------------------------------------------------------------------------
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:@"¥932452" attributes:@{
NSFontAttributeName : [UIFont systemFontOfSize:20.f],
NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#5bcec0"],
NSStrikethroughStyleAttributeName : @(NSUnderlineStyleSingle|NSUnderlinePatternSolid),
NSStrikethroughColorAttributeName : [UIColor colorWithHexString:@"#5bcec0"]
}];
添加的四个特征:
文字大小
文字颜色
删除线样式为单实线
删除线的颜色
2、通过sizeToFit改变frame——竖直排列
CGRect frame =CGRectMake(kScreenWidth/2,20,30,30);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.backgroundColor = [UIColor orangeColor];
NSString*text = @"今天是个好日子啊";
label.text = text;
// 根据内容自适应尺寸
label.numberOfLines = 0;
[label sizeToFit];![Snip20180417_7.png](https://upload-images.jianshu.io/upload_images/185169-85e44fe7e44a6be0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
[self.view addSubview:label];
UILabel 竖直方向排列
1、换行
2、sizeToFit 自适应尺寸
3、文字滚动显示