新建一个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,支持十六进制颜色设置)的更多相关文章

  1. iOS开发技巧系列---详解KVC(我告诉你KVC的一切)

    KVC(Key-value coding)键值编码,单看这个名字可能不太好理解.其实翻译一下就很简单了,就是指iOS的开发中,可以允许开发者通过Key名直接访问对象的属性,或者给对象的属性赋值.而不需 ...

  2. iOS:扩展UIColor,支持十六进制颜色设置

    来自转载:http://my.oschina.net/leejan97/blog/307491 摘要: 可以直接使用十六进制设置控件的颜色,而不必通过除以255.0进行转换 #define UICol ...

  3. iOS开发技巧系列---使用链式编程和Block来实现UIAlertView

    UIAlertView是iOS开发过程中最常用的控件之一,是提醒用户做出选择最主要的工具.在iOS8及后来的系统中,苹果更推荐使用UIAlertController来代替UIAlertView.所以本 ...

  4. Web 前端开发精华文章集锦(jQuery、HTML5、CSS3)【系列十八】

    <Web 前端开发精华文章推荐>2013年第六期(总第十八期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各种增强网站用户体验的 jQuery 插件,展示前沿的 HTML5 和 C ...

  5. iOS 11开发教程(十八)iOS11应用视图之使用代码添加按钮

    iOS 11开发教程(十八)iOS11应用视图之使用代码添加按钮 由于使用编辑界面添加视图的方式比较简单,所以不在介绍.这里,直接讲解代码中如何添加.使用代码为主视图添加一个按钮的方式和在1.3.3节 ...

  6. 【转】iOS开发工具系列(按功能分)

    http://www.cocoachina.com/newbie/basic/2014/0417/8187.html 这是我们多篇iOS开发工具系列篇中的一篇,此前的文章比如:那些不能错过的Xcode ...

  7. 【转】几点 iOS 开发技巧

    [译] 几点 iOS 开发技巧 原文:iOS Programming Architecture and Design Guidelines 原文来自破船的分享 原文作者是开发界中知晓度相当高的 Mug ...

  8. Senparc.Weixin.MP SDK 微信公众平台开发教程(十八):Web代理功能

    在Senparc.Weixin.dll v4.5.7版本开始,我们提供了Web代理功能,以方便在受限制的局域网内的应用可以顺利调用接口. 有关的修改都在Senparc.Weixin/Utilities ...

  9. iOS开发Swift篇—(八)函数(2)

    iOS开发Swift篇—(八)函数(2) 一.函数类型 函数类型也是数据类型的一种,它由形参类型和返回值类型组成,格式是 (形参类型列表) -> 返回值类型 func sum(num1: Int ...

随机推荐

  1. Uva 12361 File Retrieval 后缀数组+并查集

    题意:有F个单词,1 <= F <=60 , 长度<=10^4, 每次可以输入一个字符串,所有包含该字串的单词会形成一个集合. 问最多能形成多少个不同的集合.集合不能为空. 分析:用 ...

  2. Android 透明Button

    1.是制作9-patch的图片,这样能够匹配文字内容的长短. 2.是指定按钮样式背景,即定制drawable的xml文件,这样做的好处不用图片做背景,节省空间. 定制透明样式的按钮.直接看代码: dr ...

  3. -fembed-bitcode is not supported on versions of iOS prior to 6.0 问题修复

    参考一下网址 http://stackoverflow.com/questions/30867544/fembed-bitcode-is-not-supported-on-versions-of-io ...

  4. hadoop2.2编程:从default mapreduce program 来理解mapreduce

    下面写一个default mapreduce 的程序: import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapr ...

  5. bzoj1054

    弱弱的搜索题, 我的做法是将矩阵看做二进制然后用位运算来做的,感觉比较舒服 ..] ,,,);       dy:..] ,,-,); type node=record        po,next: ...

  6. cmd for 用法

    1:for命令及变量 基本格式: FOR /参数 %variable IN (set) DO command [command_parameters] %variable:指定一个单一字母可替换的参数 ...

  7. gif压缩

    保存选择颜色值小点会小很多

  8. poj 1084 舞蹈链(纠结题)

    此题反正我自己是认为poj给的数据范围是有错的,不知道是不是自己太弱了,有大神在的话,欢迎来呸! 其实目的就在于建图,搞的我后来建了一个无比纠结的图,先建立了火柴棍和正方形的一个全图,然后再删除一些火 ...

  9. Spring注解@Component、@Repository、@Service、@Controller,@Autowired、@Resource用法

    一.Spring定义bean,@Component.@Repository.@Service 和 @Controller Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥 ...

  10. remove all event handlers from a control

    The sample code below will remove all Click events from button1 public partial class Form1 : Form { ...