1.
UILabel *lbl_oldPrice = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 30)];
lbl_oldPrice.font = [UIFont systemFontOfSize:14];
lbl_oldPrice.backgroundColor = [UIColor greenColor];
NSString *textStr = @"¥199";
//中劃線
NSDictionary *attribtDic1 = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
NSMutableAttributedString *attribtStr1 = [[NSMutableAttributedString alloc]initWithString:textStr attributes:attribtDic1];
lbl_oldPrice.attributedText = attribtStr1;
// 下劃線
NSDictionary *attribtDic2 = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
NSMutableAttributedString *attribtStr2 = [[NSMutableAttributedString alloc]initWithString:textStr attributes:attribtDic2];
lbl_oldPrice.attributedText = attribtStr2;
lbl_oldPrice.attributedText = attribtStr1;
lbl_oldPrice.textColor = [UIColor grayColor];
lbl_oldPrice.textAlignment = NSTextAlignmentLeft;
[self.view addSubview:lbl_oldPrice];
2.
iOS10.3更新后陨享,商城APP這樣的UI:原價(jià) “¥500 ” 類似Label設(shè)置的中劃線突然失效了宫峦。
這可能是蘋果系統(tǒng)的一個(gè)bug。
根本原因:UILabel上的文字只要包含有“中文”囊蓝,富文本字符串的中劃線就會失效劫扒,我們可通過以下兩種方式解決。
第一種方式:人民幣符號“¥”和“¥”荡陷,使用前面一個(gè)即可雨效。
NSString *market = [NSString stringWithFormat:@"¥%@",@"500"];
NSMutableAttributedString *attributeMarket = [[NSMutableAttributedString alloc] initWithString:market];
[attributeMarket setAttributes:@{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]} range:NSMakeRange(0,market.length)];
_marketLabel.attributedText = attributeMarket;
第二種方式:讓富文本支持“中文”
增加一個(gè)富文本屬性: NSBaselineOffsetAttributeName : @(NSUnderlineStyleSingle)
NSString *market = [NSString stringWithFormat:@"¥%@",@"500"];
NSMutableAttributedString *attributeMarket = [[NSMutableAttributedString alloc] initWithString:market];
[attributeMarket setAttributes:@{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSBaselineOffsetAttributeName : @(NSUnderlineStyleSingle)} range:NSMakeRange(0,market.length)];
_marketLabel.attributedText = attributeMarket;