新建一個Category,命名爲UIColor+Hex,表示UIColor支持十六進制Hex顏色設置。
app
UIColor+Hex.h文件,ide
#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) / 255.0f) alpha:1.0f] @interface UIColor (Hex) + (UIColor *)colorWithHexString:(NSString *)color; //從十六進制字符串獲取顏色, //color:支持@「#123456」、 @「0X123456」、 @「123456」三種格式 + (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha; @end
上面的代碼在開頭是兩個宏定義,就是對[UIColor colorWithRed:green:blue:alpha]方法的簡化,在UIColor(Hex)中聲明兩個方法-colorWithHexString和-colorWithHexString:alpha,這個很好理解。spa
UIColor+Hex.m文件code
#import "UIColor+Hex.h" @implementation UIColor (Hex) + (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha { //刪除字符串中的空格 NSString *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 //若是是0x開頭的,那麼截取字符串,字符串從索引爲2的位置開始,一直到末尾 if ([cString hasPrefix:@"0X"]) { cString = [cString substringFromIndex:2]; } //若是是#開頭的,那麼截取字符串,字符串從索引爲1的位置開始,一直到末尾 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]; } //默認alpha值爲1 + (UIColor *)colorWithHexString:(NSString *)color { return [self colorWithHexString:color alpha:1.0f]; } @end
這樣就擴展了UIColor,支持十六進制顏色設置。下面舉個栗子,設置UIButton一些顏色特徵,來講明該擴展的使用,orm
#import "UIColor+Hex.h" //省略多餘的代碼 //設置導航欄右側的BarButtonItem爲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, 44); rightButton.backgroundImageEdgeInsets = UIEdgeInsetsMake(7, 0, 7, 0); //kSetting是國際化的字符串"設置" [rightButton setTitle:NVSLocalizedString(@"kSetting", nil) forState:UIControlStateNormal]; //使用宏定義的RGB_COLOR // [rightButton setTitleColor:RGB_COLOR(160, 170, 150) forState:UIControlStateHighlighted]; //使用UIColor+Hex擴展 [rightButton setTitleColor:[UIColor colorWithHexString:@"#708c3b"] forState:UIControlStateNormal]; rightButton.titleLabel.font = [UIFont fontWithName:@"Heiti 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]; }
恩,使用差很少就這麼簡單,總結一下,本篇博客主要有如下幾個細節或者說知識點,索引
(1)宏定義RGB_COLOR和RGBA_COLOR能夠設置顏色ip
(2)UIColor+Hex擴展能夠設置顏色字符串
(3)導航欄上面的BarButtonItem怎麼設置爲Buttonget
(4)Button一些經常使用和不經常使用的屬性設置博客