您的位置:首页 > 产品设计 > UI/UE

UILabel显示不同颜色文字。

2014-01-22 12:45 344 查看
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;

效果如图:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: