iOS开发UILabel篇:iOS 10.3 Label设置的中划线突然失效了

来源:互联网 发布:最好的地图软件 编辑:程序博客网 时间:2024/05/07 09:01

iOS10.3更新后,商城APP这样的UI:原价 “¥500 ” 类似Label设置的中划线突然失效了。

这可能是苹果系统的一个bug。

根本原因:Label上的文字只要包含有“中文”,富文本字符串的中划线就会失效,我们可通过以下两种方式解决。

第一种方式:人民币符号“¥”和“¥”,使用前面一个即可。

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;

第二种方式:让富文本支持“中文”

增加一个富文本属性:
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;

到此,目前你可以根据需求暂时选一种方式来解决,相信苹果会在下一版本解决这个bug。

1 0