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 ...
随机推荐
- 汇编语言第二版 程序在dos中执行情况.P86-87
假设程序要被dos系统加载到sa:0000的内存中,在这个地址的内存开始会有256个字节的PSP程序,用于加载程序和dos系统的通信.ds中的地址为sa. 真正的程序会在这256个字节之后.所以真正程 ...
- [水题]Codeforces337A Puzzles
题目链接 题意:要在m个数里面选n个数, 要求这n个数的差值要最小 题意在hint里很清晰了 这道题从题意到题目本身都没有什么trick 写这道题完全是为了用一下#include <numeri ...
- [unity菜鸟] 笔记1 —— 函数篇
SendMessage() 调用其他物体中的指令,先在脚本中编写一个自定义的函数,然后使用SendMessage()命令来调用那个物体上的命令 //①将以下函数附给target对象 void Rena ...
- LinearLayout按下(pressed)或获取焦点(focused)时背景设置不同颜色或图片
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id=&qu ...
- Android开发UI之ViewPager及PagerAdapter
ViewPager,官网链接--http://developer.android.com/reference/android/support/v4/view/ViewPager.html ViewPa ...
- Android开发UI之自定义动画
自定义动画,需要新建一个类,继承Animation类. 重写applyTransformation()方法和initialize()方法. applyTransformation(float inte ...
- 转:Apache和Nginx运行原理解析
Web服务器 Web服务器也称为WWW(WORLD WIDE WEB)服务器,主要功能是提供网上信息浏览服务. 应用层使用HTTP协议. HTML文档格式. 浏览器统一资源定位器(URL). Web服 ...
- BZOJ_1005_ [HNOI2008]_明明的烦恼_(组合数学+purfer_sequence+高精度+分解因数+快速幂)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1005 一棵树有n个点,给出没给节点的度,如果没有限制则为-1,求共有多少种可能的树. 分析 蒟 ...
- C#实现调用Java类中方法
基本思路: 用C#实现调用Java编写的类中的方法:重点是将Java编写的程序打包成Jar,然后使用开源工具IKVM将其转化成DLL控件,在.NET环境下调用. 分为以下步骤: 1.下载JDK6(注: ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.5
Show that matrices with distinct eigenvalues are dense in the space of all $n\times n$ matrices. (Us ...