Create a new category named Uicolor+hex, which indicates that the Uicolor supports hex hex color settings.
Uicolor+hex.h file,
#import <UIKit/UIKit.h> #define Rgba_color (R, G, B, A) [Uicolor colorwithred: ((r)/255.0f) Green: ((G)/255.0f) Blue :((b)/255.0f) ALPHA:A] #define Rgb_color (R, G, B) [Uicolor colorwithred: ((r)/255.0f) Green: ((G)/255.0f) Blue: ((b)/2 55.0f) alpha:1.0f] @interface Uicolor (hex) + (Uicolor *) colorwithhexstring: (NSString *) color;//get color from hex string,//color: Support @ "#123456", @ "0x123456", @ "123456" three formats + (Uicolor *) colorwithhexstring: (NSString *) color alpha: (cgfloat) Alpha; @end
The above code begins with two macro definitions, which is the simplification of the [Uicolor ColorWithRed:green:blue:alpha] method, in Uicolor (Hex) Two methods are declared in-colorwithhexstring and-colorwithhexstring:alpha, which is well understood.
UICOLOR+HEX.M file
#import "Uicolor+hex.h" @implementation UIColor (Hex) + (uicolor *) colorwithhexstring: ( nsstring *) Color alpha: (cgfloat) alpha{ //Delete spaces in string nsstring *cstring = [[color stringbytrimmingcharactersinset:[nscharacterset Whitespaceandnewlinecharacterset]] uppercasestring]; // string should be 6 or 8 characters if ([cstring length] < &NBSP;6) { return [UIColor clearcolor]; } // strip 0x if it appears //if it starts with 0x, then the string is truncated, starting at index 2 and ending at 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 substrings NSRange range; range.location = 0; range.length = 2; //r nsstring *rstring = [cString substringWithRange:range]; //g range.location = 2; nsstring *gstring = [cstring substringwithrange:range]; //b range.location = 4; nsstring *bstring = [cstring substringwithrange:range]; // scan values unsigned int r, 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&NBSP;ALPHA:1.0F];} @end
This expands the uicolor and supports hexadecimal color settings. Here is a chestnut, set UIButton some color features to illustrate the use of the extension,
#import "uicolor+hex.h"//Omit extra code//set Barbuttonitem on the right side of the navigation bar to button- (void) setupnavigationitem{ UIView *rightView = [[UIView alloc] init]; rightview.bounds = cgrectmake (0, 0, 52, 44); uibutton *rightbutton = [uibutton buttonwithtype: Uibuttontypecustom]; rightbutton.frame = cgrectmake ( -6, 0, 52, &NBSP;44); rightbutton.backgroundimageedgeinsets = uiedgeinsetsmake (7, 0 , 7, 0); //ksetting is an internationalized string "Settings" [rightButton Settitle:nvslocalizedstring (@ "ksetting", nil) forState:UIControlStateNormal]; //uses the Rgb_color// [rightbutton settitlecolor:rgb_color defined by the macro (160, 170, 150) forstate:uicontrolstatehighlighted]; //using Uicolor+hex extension [rightbutton settitlecolor:[uicolor colorwithhexstring:@ "#708c3b"] forstate:uicontrolstatenormal]; rightbutton.titlelabel.font = [uifont fontwithname:@ "HEITI&NBSP;SC" size:12.f]; [rightbutton setbackgroundimage:[uiimage imagenamed:@ "DEVICE_SETTING_BG"] forstate:uicontrolstatenormal]; [ rightbutton setbackgroundimage:[uiimage imagenamed:@ "Device_setting_bg_press"] forstate:uicontrolstatehighlighted]; [rightbutton addtarget:self action: @selector (settingBTNPRESSS:) forcontrolevents: uicontroleventtouchupinside]; [rightview addsubview:rightbutton]; uibarbuttonitem *rightbarbuttonitem = [[uibarbuttonitem alloc] initWithCustomView:rightView]; [self.navigationItem Setrightbarbuttonitem:rightbarbuttonitem animated:yes]; [rightbarbuttonitem release]; [rightview release];}
Well, the use of almost as simple, summed up, this blog mainly has the following several details or knowledge points,
(1) macro definition Rgb_color and Rgba_color can set color
(2) Uicolor+hex extension can set color
(3) How the Barbuttonitem above the navigation bar is set to button
(4) button some common and infrequently used property settings
iOS Development Tips (Series 18: Extended Uicolor, hex color settings supported)