iOS中的字符串扫描类NSScanner

时间:2023-03-09 17:02:17
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一些常用和不常用的属性设置