UILabel显示不同颜色文字。

 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显示不同颜色文字。

上一篇:研磨设计模式之桥接模式-1


下一篇:GalaxyEngine Overview