NSString的特殊用法

1.利用NSMutableAttributedString修改字符串的颜色

+ (NSMutableAttributedString *)attributedStringWithTitle:(NSString *)fitstTitle SecondTitle:(NSString *)secondTitle firstColor:(UIColor *)firstColor secondColor:(UIColor *)secondColor
{
   NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@",fitstTitle,secondTitle]];
   [str addAttribute:NSForegroundColorAttributeName value:firstColor range:NSMakeRange(0,fitstTitle.length)];
    [str addAttribute:NSForegroundColorAttributeName value:secondColor range:NSMakeRange(fitstTitle.length,secondTitle.length)];
   return str;
}

2.利用NSMutableAttributedString修改字符串的字体

+ (NSMutableAttributedString *)attributedStringWithTitle:(NSString *)fitstTitle SecondTitle:(NSString *)secondTitle firstFont:(UIFont *)firstFont secondfont:(UIFont *)secondFont
{
   NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@",fitstTitle,secondTitle]];
   [str addAttribute:NSFontAttributeName value:firstFont range:NSMakeRange(0,fitstTitle.length)];
   [str addAttribute:NSFontAttributeName value:secondFont range:NSMakeRange(fitstTitle.length,secondTitle.length)];
   [str addAttribute:NSForegroundColorAttributeName value:ZFColor_ox(0x333333) range:NSMakeRange(0,fitstTitle.length)];
   [str addAttribute:NSForegroundColorAttributeName value:ZFColor_ox(0x333333) range:NSMakeRange(fitstTitle.length,secondTitle.length)];
   NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
   [paragraphStyle setLineSpacing:[ZFUtility getAllViewHeight:20]];
   [str addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [fitstTitle length])];
   return str;
}

3. 字符串隔三位加特殊符号

/**
 隔三位加符号

 @param numbers <#numbers description#>
 */
+ (NSString *)hanleNums:(NSString *)numbers{
    NSString *str = [numbers substringWithRange:NSMakeRange(numbers.length%3, numbers.length-numbers.length%3)];
    NSString *strs = [numbers substringWithRange:NSMakeRange(0, numbers.length%3)];
    for (int  i =0; i < str.length; i =i+3) {
        NSString *sss = [str substringWithRange:NSMakeRange(i, 3)];
        strs = [strs stringByAppendingString:[NSString stringWithFormat:@",%@",sss]];
    }
    if ([[strs substringWithRange:NSMakeRange(0, 1)] isEqualToString:@","]) {
        strs = [strs substringWithRange:NSMakeRange(1, strs.length-1)];
    }
    return strs;
}

你可能感兴趣的:(NSString的特殊用法)