在ios10.3的系統下使用刪除線失效

前幾天使用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。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容