label 不同颜色

label  不同颜色

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];

posted @ 2016-08-12 10:47  zuidap  阅读(217)  评论(0编辑  收藏  举报