iOS -XX天xx時xx分xx秒部分改變顏色和字型大小
這個就是一個簡單的NSString改變為NSAttributedString後
對字串進行rang查詢和顏色替換。
直接上程式碼。
- (NSAttributedString *)transformString:(NSString *)string{
NSMutableAttributedString *textColor = [[NSMutableAttributedStringalloc]initWithString:string];
[textColor addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:
[textColor addAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor] range:NSMakeRange(0, string.length)];
if ([string containsString:@"天"]) {
NSRange range = [string rangeOfString:@"天"];
[textColor addAttribute:NSForegroundColorAttributeNamevalue
[textColor addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:16] range:range];
}
if ([string containsString:@"時"]) {
NSRange range = [string rangeOfString:@"時"];
[textColor addAttribute:NSForegroundColorAttributeNamevalue
[textColor addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:16] range:range];
}
if ([string containsString:@"分"]) {
NSRange range = [string rangeOfString:@"分"];
[textColor addAttribute:NSForegroundColorAttributeNamevalue:[UIColorgrayColor] range:range];
[textColor addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:16] range:range];
}
if ([string containsString:@"秒"]) {
NSRange range = [string rangeOfString:@"秒"];
[textColor addAttribute:NSForegroundColorAttributeNamevalue:[UIColorgrayColor] range:range];
[textColor addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:16] range:range];
}
return textColor;
}