label 字体不同颜色封装(个人笔记) NSMutableAttributedString

个人笔记,不喜勿喷

超.png


lab1.attributedText  = [下边的方法];
- (NSMutableAttributedString *)getStrWith:(NSString *)str1
                                     str2:(NSString *)changeStr
                                     str3:(NSString *)str3
                                    color:(UIColor *)color{
    
    NSInteger length1 = str1.length;
    NSInteger length2 = changeStr.length;
    NSString *allStr = [NSString stringWithFormat:@"%@%@%@",str1,changeStr,str3];
    NSInteger length3 = allStr.length;
    
    NSMutableAttributedString *resultStr = [[NSMutableAttributedString alloc] initWithString:allStr];
    [resultStr addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(length1, length2)];
    
    return resultStr;
}

你可能感兴趣的:(label 字体不同颜色封装(个人笔记) NSMutableAttributedString)