iOS中的字符串扫描类NSScanner
新建一个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的更多相关文章
- [Swift]Scanner字符串扫描类
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- IOS中把字符串加密/IOS中怎么样MD5加密/IOS中NSString分类的实现
看完过后,你会学到: 1学习IOS开发中的分类实现, 2以及类方法的书写, 3以及字符串的MD5加密/解密. ---------------------------wolfhous---------- ...
- 在iOS中使用ZBar扫描二维码
最近在做的项目中需要用到二维码扫描功能,之前在Android中使用过ZXing识别二维码,ZXing也有对应的iOS版本,经过了解,ZBar也是一个常用的二维码识别软件,并分别提供了iOS和Andro ...
- iOS中的字符串NSString
创建一个字符串对象: NSstring * str1 = @"hello world"; NSString * str = [[NSString alloc]initWithStr ...
- iOS中编写单例类的心得
单例 1.认识过的单例类有哪些: NSUserDefaults.NSNotificationCenter.NSFileManager.UIApplication 2.单例类 单例类某个类在代码编写时使 ...
- iOS中截取字符串指定位置
直接上代码: NSString *string = @"今天是个好日子,忘记穿秋裤了"; NSString *string1 = [];//截取掉下标7之后的字符串 NSStrin ...
- 在iOS中使用ZBar扫描二维码和条形码
最近做了个外包项目,里面用到了二维码扫描和微信支付!之前比较熟悉的是ZXing,但是在Xcode7.1里面发现竟然莫名的不支持,木有办法,从网上查了一下还有一种支持二维码扫描的东西,没错就是接下来我要 ...
- iOS中计算字符串NSString的高度
根据固定宽度计算字符串高度: NSString *info = @"但是公司的高度是广东省公司的广东省高速度来开个大帅哥多撒谎个爱好就跟他说噶三公司噶是的刚好是我哥如果黑暗如果坏都干撒降低公 ...
- Java 中 json字符串转换为类
使用到alibaba.fastjson包 具体实现 JSONObject jsonObject = JSONObject.parseObject(msg); SmsSenderStatus smsSe ...
随机推荐
- [topcoder]SmartWordToy
广度搜索BFS,要用Queue.还不是很熟,这道题帮助理清一些思绪了.其实这道题是求最短路径,所以BFS遇到第一个就可以返回了,所以后面有些现有大小和历史大小的判断可以省却. 过程中拿数组存step还 ...
- android & Linux uevent机制
Linux uevent机制 Uevent是内核通知android有状态变化的一种方法,比如USB线插入.拔出,电池电量变化等等.其本质是内核发送(可以通过socket)一个字符串,应用层(andro ...
- 如何从Win7中提取制作Windows PE3.0
在D盘新建文件夹winpe,在winpe中新建sources.pe3和new文件夹,把附件中提供的工具imagex连文件夹一起放到winpe目录中. 制作方法: 1.把windows7光盘(或光盘镜像 ...
- Extend ComboGrid Editors for DataGrid Of JQuery EasyUI
在JQueryEasyUI中为DataGrid自定义了一个ComboGrid编辑器.具体方法: 自己写一个扩展 $.extend($.fn.datagrid.defaults.editors, { c ...
- matlab添加M_map工具箱(转 http://blog.sina.com.cn/s/blog_491b86bf0100srt9.html)
之前转载过matlab画世界地图的博文.最近正好用到.首先试了matlab自带的worldmap,感觉画出来的图形不尽如人意,比较杂乱.如下图. 略查阅了些资料,请教了Liangjing,一致推荐m_ ...
- Android学习之路——简易版微信为例(三)
最近好久没有更新博文,一则是因为公司最近比较忙,另外自己在Android学习过程和简易版微信的开发过程中碰到了一些绊脚石,所以最近一直在学习充电中.下面来列举一下自己所走过的弯路: (1)本来打算前端 ...
- Android What is Application
本文转自:http://www.cnblogs.com/elleniou/archive/2012/05/16/2502661.html Application和Activity,Service一样是 ...
- arcgis for android访问arcgis server上自己制作部署的地图服务
转自:http://gaomw.iteye.com/blog/1110437 本项目的开发环境是eclipse3.5 + ADT11插件+arcgis for andorid 插件 + arcgis ...
- [Raobin] Ext.net在页面中以窗体的形式打开另外的页面
public ActionResult NewPartWindow(string id, string copy, string code) { var vm = new VM(this); var ...
- 【译】Selenium 2.0 WebDriver
Selenium WebDriver 注意:我们正致力于完善帮助指南的每一个章节,虽然这个章节仍然存在需要完善的地方,不过我们坚信当前你看到的帮助信息是精确无误的,后续我们会提供更多的指导信息来完 ...