iOS label设置不同的字体 颜色

 UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 70, 320, 40)];
    lable.numberOfLines = 0;
//    lable.backgroundColor = [UIColor greenColor];
    [self.view addSubview:lable];
    
    NSString *string =
    @"我是好人"@"我是好人"@"我是个好人"@"额鹅鹅鹅鹅鹅鹅饿"@"哇哇哇哇哇哇哇";
    // 创建可变属性化字符串
    NSMutableAttributedString *attrString =
    [[NSMutableAttributedString alloc] initWithString:string];
    //改变字符串当中从第18位置向后的10位数的字体
    UIFont *smallFont = [UIFont systemFontOfSize:12];
    [attrString addAttribute:NSFontAttributeName value:smallFont range:NSMakeRange(18, 10)];
    //改变字符串当中第一个“1”的颜色
    UIColor *rcolor = [UIColor redColor];
    
    [attrString addAttribute:NSForegroundColorAttributeName value:rcolor range:[string rangeOfString:@"鹅"]];
    lable.attributedText = attrString;

你可能感兴趣的:(小技巧类)