iOS开发UILabel篇:iOS 8 下中划线失效的解决方法

我们都知道给Label设置中划线、下划线等等,可以使用富文本NSMutableAttributedString

原价不设置,¥100 中间设置中划线

       NSString *market = @"原价:¥100"
        NSMutableAttributedString *attributeMarket = [[NSMutableAttributedString alloc] initWithString:market];
        [attributeMarket setAttributes:@{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]} range:NSMakeRange(3,market.length)];
        _marketLabel.attributedText = attributeMarket;
        _marketLabel.hidden = NO;

以上方式iOS 8上失效了,搞不清,原来得这么处理

iOS 8上必须从0开始,如下:

NSString *market = @"原价:¥100"
        NSMutableAttributedString *attributeMarket = [[NSMutableAttributedString alloc] initWithString:market];
        [attributeMarket setAttributes:@{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleNone]} range:NSMakeRange(0,3)];//**iOS 8需要加上这句**
        [attributeMarket setAttributes:@{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]} range:NSMakeRange(3,market.length)];
        _marketLabel.attributedText = attributeMarket;
        _marketLabel.hidden = NO;

如果您的问题是iOS10.3,请转至这里:iOS 10.3 Label设置的中划线突然失效了

你可能感兴趣的:(iOS开发,小技巧,中划线,失效,iOS-8,iOS8)