一个label上的文字显示不同的颜色

来源:互联网 发布:mac搭建lamp 编辑:程序博客网 时间:2024/05/22 03:43
UILabel* noteLabel = [[UILabel alloc] init];
    noteLabel.frame = CGRectMake(60, 100, 200, 100);
    noteLabel.textColor = [UIColor blackColor];
    noteLabel.numberOfLines = 2;
    
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:@"点击注册按钮,即表示您已同意隐私条款和服务协议"];
    NSRange redRange = NSMakeRange([[noteStr string] rangeOfString:@"注册"].location, [[noteStr string] rangeOfString:@"注册"].length);
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange];

    NSRange redRangeTwo = NSMakeRange([[noteStr string] rangeOfString:@"同意"].location, [[noteStr string] rangeOfString:@"同意"].length);
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:redRangeTwo];
   
    [noteLabel setAttributedText:noteStr];
    [noteLabel sizeToFit];
    [self.view addSubview:noteLabel];

图片:屏幕快照 2015-01-22 下午5.32.03.png 
0 0
原创粉丝点击