让指定位置字符串中的任意位置显示不同的颜色大小

让指定字符串中的任意位置显示不同的颜色大小

_LabelPeopleWaiting.text=[NSStringstringWithFormat:@"共有%@人在您面前排队",@"200"];

NSMutableAttributedString*str = [[NSMutableAttributedStringalloc]initWithString:_LabelPeopleWaiting.text];

//获取人数位置

NSRangerange1=[_LabelPeopleWaiting.textrangeOfString:@"人"];

NSRangerange2=[_LabelPeopleWaiting.textrangeOfString:@"有"];

[straddAttribute:NSForegroundColorAttributeName

value:YBHUIColorFromRGB(0x79be20)

range:NSMakeRange(range2.location+1,range1.location-2)];

[straddAttribute:NSFontAttributeName

value:[UIFontfontWithName:@"Arial"size:30.0]

range:NSMakeRange(range2.location+1, range1.location-2)];

_LabelPeopleWaiting.attributedText=str;






//改变字体颜色

-(void)changeTextColor:(UILabel*)labeltext textview:(UITextView*)textView

{

NSMutableAttributedString*str = [[NSMutableAttributedStringalloc]initWithString:labeltext.text];

//获取人数位置

NSRangerange1=[labeltext.textrangeOfString:@"/"];

NSRangerange2=[labeltext.textrangeOfString:@"("];

NSIntegernumberLenth;

if(textView.text.length<=9) {

numberLenth =1;

}else{

numberLenth =2;

}

DLog(@"%ld,%ld",range1.location,range2.location);

[straddAttribute:NSForegroundColorAttributeName

value:[UIColorredColor]

range:NSMakeRange(range2.location+1,numberLenth)];

labeltext.attributedText=str;

}

你可能感兴趣的:(让指定位置字符串中的任意位置显示不同的颜色大小)