iOS 給NSString文字上添加橫線 中間和下划線


有時候我們需要給文字添加橫線,有兩種情況:

第一種是貫穿中間的橫線:

橫線的顏色和文字的顏色保持一致

 _oldPriceLabel.text = @"3500";
_oldPriceLabel.textColor = [UIColor lightGrayColor];
NSMutableAttributedString *newPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%@",_oldPriceLabel.text]];
[newPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, newPrice.length)];
 _oldPriceLabel.attributedText = newPrice;

效果如圖:

第二種是給文字添加下划線:

代碼如下:

NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"忘記密碼?"];
NSRange titleRange = {0,[title length]};
[title addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:titleRange];
[_forgetBtn setAttributedTitle:title
                      forState:UIControlStateNormal];

效果如下圖:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM