ios Label显示不同颜色文字

来源:互联网 发布:centos系统 编辑:程序博客网 时间:2024/05/16 16:13
+ (NSMutableAttributedString *)stringColorStr:(NSString *)str String:(NSString *)string Color:(UIColor*)color{    //Label不同字显示不同颜色    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@",str,string]];    NSRange redRange = NSMakeRange([[noteStr string] rangeOfString:string].location, [[noteStr string] rangeOfString:string].length);    //需要设置的位置    [noteStr addAttribute:NSForegroundColorAttributeName value:color range:redRange];    return noteStr;}

调用方式:[label setAttributedText:[RRPPrintObject stringColorStr:@”不变色的字符” String:@”变色的字符” Color:变的颜色];

原创粉丝点击