IOS same Uilabel change the color of numbers

Source: Internet
Author: User

- (void) Setrichnumberwithlabel: (UILabel *) label Color: (Uicolor *) Color FontSize: (cgfloat) size{//Converts the text of a label to NsmutalbeattributedstringNsmutableattributedstring *attributedstring =[[nsmutableattributedstring alloc]initwithstring:label.text];//defining an empty stringNSString *temp =Nil;//loop based on the length of the property string     for(inti =0; i < [attributedstring length]; i++){//take 1-length strings at a timetemp = [Label.text substringwithrange:nsmakerange (I,1)];//The length of the string is interpreted to include numbers and punctuation.        if([Temp isequaltostring:@"0"] || [Temp isequaltostring:@"1"] || [Temp isequaltostring:@"2"] || [Temp isequaltostring:@"3"] || [Temp isequaltostring:@"4"] || [Temp isequaltostring:@"5"] || [Temp isequaltostring:@"6"] || [Temp isequaltostring:@"7"] || [Temp isequaltostring:@"8"] || [Temp isequaltostring:@"9"]|| [Temp isequaltostring:@"."] || [Temp isequaltostring:@"-"]){//add a color to a qualifying property string, font[attributedstring setattributes:[nsdictionary Dictionarywithobjectsandkeys:color,
Nsforegroundcolorattributename, [Uifont Fontwithname:font_lanting_jianhei size:size],
Nsfontattributename, Nil] range:nsmakerange (i,1)]; } }//re-pass the text of the label to the property string that is handled wellLabel.attributedtext =attributedstring;}

IOS same Uilabel change the color of numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.