字符串局部改变颜色

来源:互联网 发布:淘宝网打底长袖裙韩版 编辑:程序博客网 时间:2024/06/05 22:47

话不多说,直接上代码:

UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 50)];
    label.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:label];
    
    NSString *contentStr = @"世界:hello world";
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:contentStr];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 3)];
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(0, 3)];

    label.attributedText = str;

0 0
原创粉丝点击