下划线:
UILabel *underlineLabel = [[UILabel alloc] initWithFrame:(CGRectMake(10, 10, 50, 30))];
NSString *textStr = [NSString stringWithFormat:@"%@元", primeCost]; // 下划线
NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:textStr attributes:attribtDic]; //赋值
underlineLabel.attributedText = attribtStr; [self.view addSubview:underlineLabel];
中划线:
UILabel * strikeLabel = [[UILabel alloc] initWithFrame:(CGRectMake(10, 10, 50, 30))];
NSString *textStr = [NSString stringWithFormat:@"%@元", primeCost]; //中划线
NSDictionary *attribtDic = @{
NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]
};
NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:textStr attributes:attribtDic]; // 赋值
strikeLabel.attributedText = attribtStr; [self.view addSubview:strikeLabel];
重要!!!
iOS10.3更新后,商城APP这样的UI:原价 “¥500 ” 类似Label设置的中划线突然失效了。
增加一个富文本属性: 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;