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設置的中劃線突然失效了

發佈了92 篇原創文章 · 獲贊 49 · 訪問量 32萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章