Uicolor+hex.h
////uicolor+hex.h//16 Binary Color Categories////Created by Apple on 15-4-3.//Copyright (c) 2015 Apple. All rights reserved.//#import<UIKit/UIKit.h>#defineRgba_color (R, G, B, A) [Uicolor colorwithred: ((r)/255.0f) Green: ((G)/255.0f) Blue: ((B)/255.0f) ALPHA:A]#defineRgb_color (R, G, B) [Uicolor colorwithred: ((r)/255.0f) Green: ((G)/255.0f) Blue: ((B)/255.0f) alpha:1.0f]@interfaceUicolor (Hex)+ (Uicolor *) colorwithhexstring: (NSString *) color;//gets the color from the hexadecimal string,//Color: support @ "#123456", @ "0x123456", @ "123456" three formats+ (Uicolor *) colorwithhexstring: (NSString *) Color alpha: (cgfloat) Alpha;@end
Uicolor+hex.m
////UICOLOR+HEX.M//16 Binary Color Categories////Created by Apple on 15-4-3.//Copyright (c) 2015 Apple. All rights reserved.//#import "uicolor+hex.h"@implementationUicolor (Hex)+ (Uicolor *) colorwithhexstring: (NSString *) Color alpha: (cgfloat) alpha{//Delete spaces in a stringNSString *cstring =[[Color Stringbytrimmingcharactersinset:[nscharacterset Whitespaceandnewlinecharacterset]] uppercaseString]; //String should be 6 or 8 characters if([cString Length] <6) { return[Uicolor Clearcolor]; } //strip 0X If it appears//if it starts with 0x, then the string is truncated, and the string starts at the position indexed to 2, up to the end if([cString Hasprefix:@"0X"]) {cString= [CString Substringfromindex:2]; } //If it starts with #, then the string is truncated, and the string starts at the position indexed to 1, up to the end if([cString Hasprefix:@"#"]) {cString= [CString Substringfromindex:1]; } if([cString length]! =6) { return[Uicolor Clearcolor]; } //separate into R, G, b substringsNsrange Range; Range.location=0; Range.length=2; //RNSString *rstring =[cString Substringwithrange:range]; //gRange.location =2; NSString*gstring =[cString Substringwithrange:range]; //bRange.location =4; NSString*bstring =[cString Substringwithrange:range]; //Scan ValuesUnsignedintR, G, B; [[Nsscanner scannerwithstring:rstring] Scanhexint:&R]; [[Nsscanner scannerwithstring:gstring] Scanhexint:&G]; [[Nsscanner scannerwithstring:bstring] Scanhexint:&b]; return[Uicolor colorwithred: ((float) R/255.0f) Green: ((float) G/255.0f) Blue: ((float) b/255.0f) Alpha:alpha];}//The default alpha value is 1+ (Uicolor *) colorwithhexstring: (NSString *) color{return[Self Colorwithhexstring:color Alpha:1.0f];}@end
IOS16 the color of the binary settings