首页 > 其他 > 详细

UILabel显示不同颜色文字。

时间:2014-01-22 22:22:50      阅读:413      评论:0      收藏:0      [点我收藏+]

 NSString *attentionStr = NSLocalizedStringFromTable(@"zhuyi",@"InfoPlist",@"When you press “Register” you agree with our Privacy policy (http://www.polar.ru/politics) and Terms of service (http://www.polar.ru/services");

    CGSize fontSize = [attentionStr sizeWithFont:FONT24];

    //注意

    UILabel *attentionLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, sumNameTextField.frame.size.height+sumNameTextField.frame.origin.y+10, 300, 100)];

    attentionLabel.numberOfLines = fontSize.width>1125?5:3;

    attentionLabel.lineBreakMode = NSLineBreakByWordWrapping;

    attentionLabel.font = FONT24;

    attentionLabel.backgroundColor = CLEARCOLOR;

    attentionLabel.textColor = TitleColor;

    attentionLabel.textAlignment = NSTextAlignmentCenter;

    [registerScrollView addSubview:attentionLabel];

    

    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:attentionStr];

    int a = [[str string] rangeOfString:@"("].location;

    int b = [[str string] rangeOfString:@")"].location;

    [str addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(a,b-a+1)];

    

    int length2 = [str string].length-b-1;

    int c = [[str string] rangeOfString:@"(" options:NSCaseInsensitiveSearch range:NSMakeRange(b+1, length2)].location;

    int d = [[str string] rangeOfString:@")" options:NSCaseInsensitiveSearch range:NSMakeRange(b+1, length2)].location;

    [str addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(c,d-c+1)];

    

    attentionLabel.attributedText = str;


效果如图:









UILabel显示不同颜色文字。

原文:http://blog.csdn.net/so_much/article/details/18658575

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!