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