一.給文字上面加刪除線
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(20, 50, 200, 50)];
[self.view addSubview:label]; label.text = @"10.00";
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor redColor];
// 橫線的顏色跟隨label字體顏色改變
NSMutableAttributedString *newPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%@",label.text]];
[newPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, newPrice.length)];
label.attributedText = newPrice;
效果:
給文字上面加刪除線
二.UILabel實(shí)現(xiàn)圖文混排
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@"連接前請先按Fn+F4切換到藍(lán)牙連接手機(jī)/平板"];
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
// 表情圖片
attch.image = [UIImage imageNamed:@"pop_pic_tip.png"];
//設(shè)置圖片大小
attch.bounds = CGRectMake(0, 0, 17*(64/21.0),17);
// 創(chuàng)建帶有圖片的富文本
NSAttributedString *imgString = [NSAttributedString attributedStringWithAttachment:attch];
[attri appendAttributedString:imgString];
NSAttributedString *string = [[NSAttributedString alloc]initWithString:@"閃爍進(jìn)入待連接狀態(tài)" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]}];
[attri appendAttributedString:string];
self.label01 = [[UILabel alloc]init];
[self.contentView addSubview:self.label01];
self.label01.numberOfLines = 0;
[self.label01 setTextColor:[UIColor colorWithRGB:0x2f3641]];
self.label01.attributedText = attri;
[self.label01 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.contentView.mas_left).mas_offset(16);
make.right.mas_equalTo(self.contentView.mas_right).mas_offset(-16);
make.top.mas_equalTo(self.imgView.mas_bottom).mas_offset(15);
}];
效果:
UILabel的圖文混排