改变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;

    }

 

posted on 2015-07-25 15:21  芳馨幽兰  阅读(1781)  评论(0编辑  收藏  举报

导航