iOS开发技巧(系列十八:扩展UIColor,支持十六进制颜色设置)

新建一个Category,命名为UIColor+Hex,表示UIColor支持十六进制Hex颜色设置。

UIColor+Hex.h文件,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#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文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#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一些颜色特征,来说明该扩展的使用,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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一些常用和不常用的属性设置

猜你喜欢

转载自huqiji.iteye.com/blog/2248087
今日推荐