有時候我們需要給文字添加橫線,有兩種情況:
第一種是貫穿中間的橫線:
橫線的顏色和文字的顏色保持一致
_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];
效果如下圖: