[分享]iOS開發-實現UILabel顯示出不一樣顏色而且某一部分產生下劃線的效果

效果圖:
圖片描述atom

@interface XSRecommendedPrivilegeViewController ()
@property (weak, nonatomic) IBOutlet UIButton *payButton;
@property (weak, nonatomic) IBOutlet UILabel *termsLabel;//服務條款
 
@end
 
@implementation XSRecommendedPrivilegeViewController
 
-(void)viewDidLoad {
    [super viewDidLoad];
    self.payButton.layer.masksToBounds = YES;
    self.payButton.layer.cornerRadius = 7;
    
    NSString * changeString = @"《傾聽者套餐服務條款》";
    NSString * termsString = [NSString stringWithFormat:@"點擊下一步表明您贊成%@", changeString];
    NSMutableAttributedString * attString = [[NSMutableAttributedString alloc] initWithString:termsString];
    
    NSRange strRange = {[changeString length],[changeString length]};//控制下劃線的開始和結束的範圍
    [attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];//劃線
    [attString addAttributes:@{NSForegroundColorAttributeName:kColor} range:[termsString rangeOfString:changeString]];//改變範圍顏色
    [self.termsLabel setAttributedText:attString];
}
//同理改變button的內容
//[_awardDisplayBtn setAttributedTitle:str forState:UIControlStateNormal];
相關文章
相關標籤/搜索