来自转载:http://my.oschina.net/leejan97/blog/307491

摘要: 可以直接使用十六进制设置控件的颜色,而不必通过除以255.0进行转换

 
新建一个Category,命名为UIColor+Hex,表示UIColor支持十六进制Hex颜色设置。
UIColor+Hex.h文件
#import <UIKit/UIKit.h>

//RGB颜色设置
#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] < )
{
return [UIColor clearColor];
}
// strip 0X if it appears
//如果是0x开头的,那么截取字符串,字符串从索引为2的位置开始,一直到末尾
if ([cString hasPrefix:@"0X"])
{
cString = [cString substringFromIndex:];
}
//如果是#开头的,那么截取字符串,字符串从索引为1的位置开始,一直到末尾
if ([cString hasPrefix:@"#"])
{
cString = [cString substringFromIndex:];
}
if ([cString length] != )
{
return [UIColor clearColor];
} // Separate into r, g, b substrings
NSRange range;
range.location = ;
range.length = ;
//r
NSString *rString = [cString substringWithRange:range];
//g
range.location = ;
NSString *gString = [cString substringWithRange:range];
//b
range.location = ;
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(, , , ); UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(-, , , );
rightButton.backgroundImageEdgeInsets = UIEdgeInsetsMake(, , , );
//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:.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开发技巧(系列十八:扩展UIColor,支持十六进制颜色设置)

    新建一个Category,命名为UIColor+Hex,表示UIColor支持十六进制Hex颜色设置. UIColor+Hex.h文件, #import <UIKit/UIKit.h> # ...

  2. iOS -UIColor随机生成颜色的方法

    在iOS 中的UIColor拥有这么多关于颜色的类方法,对于一般常见的UI控件,我们可以通过[UIColorblackColor]设置背景色 eg:设置button 的背景色为红色 UIButton ...

  3. IOS 工程所支持的版本 设置

    如何设置 Base SDK 和 iOS Deployment Target ? http://leopard168.blog.163.com/blog/static/16847184420116159 ...

  4. iOS UITabBarItem 选中图的颜色,设置UIimage的渲染模式

    UITbarController之前有在这篇文章讲解:http://www.cnblogs.com/niit-soft-518/p/4447940.html 如果自定义了UITabBarItem的图片 ...

  5. ios oc ui 路径和颜色设置--崩溃解决方案

    - (id)init{ self = [super init]; if (self) { _lineColor = CGColorCreateCopy([UIColor whiteColor].CGC ...

  6. iOS导航栏的背景颜色设置

    方法一: (1) self.navigationController.navigationBar.barStyle = UIBarStyleDefault; self.navigationContro ...

  7. kivy里rgb,rgba颜色相关知识,以及支持十六进制颜色值方法

    rgb,rgba在kivy里的格式为如下: rgb:1,1,1---->每个参数为0-1的数值(如0.1),也就是说第一位如果是0.1意思是10%的红色,由r代表,g代表绿色,b代表蓝色 rgb ...

  8. iOS开发UITableViewCell的选中时的颜色设置(转)

    iOS开发UITableViewCell的选中时的颜色设置   1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSelectionStyle ...

  9. 【转】iOS开发UITableViewCell的选中时的颜色设置

    原文网址:http://mobile.51cto.com/hot-404900.htm 1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSe ...

随机推荐

  1. 类似web风格的 Winform 分页控件

    背景 最近做一个Winform的小程序,需要用到分页,由于之前一直在用 TonyPagerForWinForm.dll ,但该库没有源代码,网上找的也不全面,索性就准备自己改造一个.在园子里翻了一下, ...

  2. Flask 基础知识一

    Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求进行预处理,然后 ...

  3. linux基础编程 套接字socket 完整的服务器端多线程socket程序【转】

    转自:http://blog.csdn.net/ghostyu/article/details/7737203 此段程序来自我的一个项目中,稍微做了些修改,运行稳定,客户端程序比较简单所以未编写,可以 ...

  4. MSP432P401R时钟入门

    拿到msp432的板子差不多一年了,刚刚进行了开机点亮LED工程:         首先是msp432的时钟模块(CS),个人理解msp432最特色的功能应该是超低功耗和高性能的组合.432系列的时钟 ...

  5. maven修改本地仓库默认路径问题

    找到maven本地路径的settings.xml文件,如D:\Program Files\apache-maven-3.0.5\conf\settings.xml: 在settings.xml文件中增 ...

  6. linux基础了解的学习记录

    一.文件结构图 linux的储存结构为文件树 二.绝对路径.相对路径.权限 1.绝对路径: /usr/local/include       在路径的最前面是 / 开头的 使用环境:当在当前路径下想到 ...

  7. 【 Linux 网络虚拟化 】Netns

    netns 可以创建一个完全隔离的新网络环境,这个环境包括一个独立的网卡空间,路由表,ARP表,ip地址表,iptables等.总之,与网络有关的组件都是独立的. 创建网络空间: # ip netns ...

  8. 【C++】const、volatile不能修饰没有this指针的成员函数

    一般所有的成员函数都只有一个复本,当不同的对象调用成员函数时,为了区分是哪个成员在调用,会传入this指针. 当调用有const.volatile修饰的成员函数时,会相应的传入一个const.vola ...

  9. 网络编程学习笔记--1.socket可读可写条件

    转至 :http://blog.csdn.net/majianfei1023/article/details/45788591 socket可读可写条件,经常做为面试题被问,因为它考察被面试者对网络编 ...

  10. springboot 通用Mapper使用

    https://blog.csdn.net/dwf_android/article/details/79359360 https://www.cnblogs.com/larryzeal/p/58741 ...