下劃線:
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:原價(jià) “¥500 ” 類似Label設(shè)置的中劃線突然失效了。
增加一個(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;