新建一个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] < )
{
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];
} 恩,使用差不多就这么简单,总结一下,本篇博客主要有以下几个细节或者说知识点, ()宏定义RGB_COLOR和RGBA_COLOR可以设置颜色 ()UIColor+Hex扩展可以设置颜色 ()导航栏上面的BarButtonItem怎么设置为Button ()Button一些常用和不常用的属性设置

iOS中的字符串扫描类NSScanner的更多相关文章

  1. [Swift]Scanner字符串扫描类

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  2. IOS中把字符串加密/IOS中怎么样MD5加密/IOS中NSString分类的实现

    看完过后,你会学到: 1学习IOS开发中的分类实现, 2以及类方法的书写, 3以及字符串的MD5加密/解密. ---------------------------wolfhous---------- ...

  3. 在iOS中使用ZBar扫描二维码

    最近在做的项目中需要用到二维码扫描功能,之前在Android中使用过ZXing识别二维码,ZXing也有对应的iOS版本,经过了解,ZBar也是一个常用的二维码识别软件,并分别提供了iOS和Andro ...

  4. iOS中的字符串NSString

    创建一个字符串对象: NSstring * str1 = @"hello world"; NSString * str = [[NSString alloc]initWithStr ...

  5. iOS中编写单例类的心得

    单例 1.认识过的单例类有哪些: NSUserDefaults.NSNotificationCenter.NSFileManager.UIApplication 2.单例类 单例类某个类在代码编写时使 ...

  6. iOS中截取字符串指定位置

    直接上代码: NSString *string = @"今天是个好日子,忘记穿秋裤了"; NSString *string1 = [];//截取掉下标7之后的字符串 NSStrin ...

  7. 在iOS中使用ZBar扫描二维码和条形码

    最近做了个外包项目,里面用到了二维码扫描和微信支付!之前比较熟悉的是ZXing,但是在Xcode7.1里面发现竟然莫名的不支持,木有办法,从网上查了一下还有一种支持二维码扫描的东西,没错就是接下来我要 ...

  8. iOS中计算字符串NSString的高度

    根据固定宽度计算字符串高度: NSString *info = @"但是公司的高度是广东省公司的广东省高速度来开个大帅哥多撒谎个爱好就跟他说噶三公司噶是的刚好是我哥如果黑暗如果坏都干撒降低公 ...

  9. Java 中 json字符串转换为类

    使用到alibaba.fastjson包 具体实现 JSONObject jsonObject = JSONObject.parseObject(msg); SmsSenderStatus smsSe ...

随机推荐

  1. UVA 10651 Pebble Solitaire 状态压缩dp

    一开始还在纠结怎么表示一个状态,毕竟是一个串.后来搜了一下题解发现了这里用一个整数的前12位表示转态就好了 ,1~o,0~'-',每个状态用一个数来表示,然后dp写起来就比较方便了. 代码: #inc ...

  2. 使用Sqoop从mysql向hdfs或者hive导入数据时出现的一些错误

    1.原表没有设置主键,出现错误提示: ERROR tool.ImportTool: Error during import: No primary key could be found for tab ...

  3. Android:MD5加密

    /** * @author gongchaobin * * MD5加密 * * @version 2013-8-22 */ public class MD5Util { // 用来将字节转换成 16 ...

  4. ipconfig命令

    C:\Windows\System32>ipconfig -all Windows IP 配置 主机名 . . . . . . . . . . . . . : LuJunTao 主 DNS 后缀 ...

  5. Linux -- Ubuntu搭建java开发环境

    Steps 1 Check to see if your Ubuntu Linux operating system architecture is 32-bit or 64-bit, open up ...

  6. com.google.common.eventbus.EventBus介绍

    以下内容直接翻译了EventBus的注释: com.google.common.eventbus.EventBus介绍: 首先这个类是线程安全的, 分发事件到监听器,并提供相应的方式让监听器注册它们自 ...

  7. Add controls dynamically in flowlayoutpanel

    For a FlowLayoutPanel, you don't need to specify a location since the controls are arranged for you. ...

  8. 用excel打造报表查询系统

    网络数据库以及ERP在中小型企业中日益风行,虽然ERP功能强大,但有的ERP报表系统中规范的报表较少,主要提供二次开发接口或通过如CRYSTALREPORT等其他报表工具进行管理,其实我们可以使用Ex ...

  9. Cookie的Domain

    每个Cookie都有常用的几个元素:name.value.expires.domain Cookie的Domain 设置cookies时,可以设置cookie的域名参数domain,标识cookie在 ...

  10. HDU 2817 A sequence of numbers

    http://acm.hdu.edu.cn/showproblem.php?pid=2817 __int64 pow_mod (__int64 a, __int64 n, __int64 m)快速幂取 ...