/** 改變label文字中某段文字的顏色,大小 label 傳入label(傳入前要有文字) oneW 從第一個文字開始 twoW 到最后一個文字結束 color 顏色 size 尺寸 */ +(void)LabelAttributedString:(UILabel*)label firstW:(NSString *)oneW toSecondW:(NSString *)twoW color:(UIColor *)color size:(CGFloat)size{ // 創建Attributed NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:label.text]; // 需要改變的第一個文字的位置 NSUInteger firstLoc = [[noteStr string] rangeOfString:oneW].location; // 需要改變的最后一個文字的位置 NSUInteger secondLoc = [[noteStr string] rangeOfString:twoW].location+1; // 需要改變的區間 NSRange range = NSMakeRange(firstLoc, secondLoc - firstLoc); // 改變顏色 [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:range]; // 改變字體大小及類型 [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-BoldOblique" size:size] range:range]; // 為label添加Attributed [label setAttributedText:noteStr]; }