標籤:
建立一個Category,命名為UIColor+Hex,表示UIColor支援十六進位Hex顏色設定。
UIColor+Hex.h檔案,
#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,這個很好理解。
UIColor+Hex.m檔案
#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一些顏色特徵,來說明該擴充的使用,
#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可以設定顏色
(2)UIColor+Hex擴充可以設定顏色
(3)導覽列上面的BarButtonItem怎麼設定為Button
(4)Button一些常用和不常用的屬性設定
iOS開發技巧(系列十八:擴充UIColor,支援十六進位顏色設定)