Set color by # color value

Source: Internet
Author: User

1+ (Uicolor *) Colorwithrgb: (NSString *) rgbstr{2NSString *newrgbstr =[Rgbstr uppercasestring];3Nsinteger strlen =newrgbstr.length;4Uicolor *color =Nil;5Unichar first = [Newrgbstr characteratindex:0];6     if(First = ='#') {7          for(inti =1; i < strlen; i++) {8Unichar U =[Newrgbstr characteratindex:i];9             if(! (U >='0'&& u <='9') &&! (U >='A'&& u <='F')) {TenNSString *exname = [NSString stringWithFormat:@"get the specified color error using the%@ class", Nsstringfromclass ([selfclass])]; OneNSException *e = [nsexception exceptionwithname:exname reason:@"Please check the color value out of bounds"Userinfo:nil]; A                 @throwe; -             } -         } the         if(Newrgbstr.length = =4) { -Unichar uarray[3]; -              for(inti =0; I <3; i++){ -Uarray[i] = [newrgbstr characteratindex: (i+1)]; +             } -              for(inti =0; I <3; i++) { +Uarray[i] = (Uarray[i] >'9'? (uarray[i]-'A'+Ten): (uarray[i]-'0')) * -% the; A             } atCGFloat r = uarray[0]/255.0f; -CGFloat g = uarray[1]/255.0f; -CGFloat B = uarray[2]/255.0f; -color = [Uicolor colorwithred:r green:g blue:b Alpha:1]; -}Else if(Newrgbstr.length = =7){ -Unichar uarray[6]; in              for(inti =0; I <6; i++){ -Uarray[i] = [newrgbstr characteratindex: (i+1)]; to             } +              for(inti =0; I <3; i++) { -Unichar L = uarray[2*i]; theUnichar r = uarray[2*i+1]; *Uarray[i] = ((L >'9'? (L-'A'+Ten): (l'0')) * -+ $(R >'9'? (r'A'+Ten): (R'0'))) % the;Panax Notoginseng             } -CGFloat r = uarray[0]/255.0f; theCGFloat g = uarray[1]/255.0f; +CGFloat B = uarray[2]/255.0f; Acolor = [Uicolor colorwithred:r green:g blue:b Alpha:1]; the         } +     } -     returncolor; $}

Color values can be set in two ways with #fff and #ffffff, and design coordination is more convenient

Set color by # color value

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.