改变label中的某字体颜色

 NSString *allString=@"你家在哪里,需要我送你么";

    NSString *subString=@"在哪里";

    UILabel *stringLabel=[[UILabel alloc]init];

    stringLabel.frame=CGRectMake(0, 100, 300, 30);

    stringLabel.font=[UIFont systemFontOfSize:17];

    stringLabel.backgroundColor=[UIColor blackColor];

    stringLabel.text=allString;

    stringLabel.textColor=[UIColor whiteColor];

    [self.view addSubview:stringLabel];

    

    NSString *allS=[allString lowercaseString];

    NSString *subS=[subString lowercaseString];

    NSRange range=[allS rangeOfString:subS];

    if (range.location!=NSNotFound) {

        NSMutableAttributedString  *string=[[NSMutableAttributedString alloc]initWithString:allString];

        [string addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:range];

        stringLabel.attributedText=string;

    }

原文地址:https://www.cnblogs.com/106dapeng/p/4675987.html