前幾天使用ios 10.3跑自己的項目程序,結果發現使用富文本為label添加刪除線全部失效,但是在10.3系統以下就一切正常,這應該是蘋果系統的一個bug。
根本原因:Label上的文字只要包含有“中文”,富文本字符串的中劃線就會失效,我們可通過以下兩種方式解決。
第一種方式:人民幣符號“¥”和“¥”,使用前面一個即可。
NSString*market = [NSStringstringWithFormat:@"¥%@",@"500"];
NSMutableAttributedString *attributeMarket = [[NSMutableAttributedString alloc] initWithString:market];
[attributeMarket setAttributes:@{NSStrikethroughStyleAttributeName: [NSNumbernumberWithInteger:NSUnderlineStyleSingle]} range:NSMakeRange(0,market.length)];
_marketLabel.attributedText= attributeMarket;
第二種方式:讓富文本支持“中文”
增加一個富文本屬性:
NSBaselineOffsetAttributeName : @(NSUnderlineStyleSingle)
NSString*market = [NSStringstringWithFormat:@"¥%@",@"500"];
NSMutableAttributedString *attributeMarket = [[NSMutableAttributedString alloc] initWithString:market];
[attributeMarket setAttributes:@{NSStrikethroughStyleAttributeName: [NSNumbernumberWithInteger:NSUnderlineStyleSingle], NSBaselineOffsetAttributeName : @(NSUnderlineStyleSingle)} range:NSMakeRange(0,market.length)];
_marketLabel.attributedText= attributeMarket;
我這邊使用的是第二種方法,因為項目中人民幣符號是由服務端傳過來的,因此第二種方法適合我自己,其他可以按情況使用一種方法解決這個bug,蘋果應該會在下個版本中修復這個bug。