您的位置:首页 > 移动开发 > IOS开发

十六进制颜色转换

2016-04-11 00:00 459 查看
UI一般标注的颜色为16进制的,而Xcode一般都是使用RGB颜色。

我给UIColor写了个类别

//   .h文件
#import <UIKit/UIKit.h>

@interface UIColor (HEX)
+ (UIColor *)getHexColor:(NSString *)hexColor;

@end

//  .m文件
#import "UIColor+HEX.h"

@implementation UIColor (HEX)

#pragma mark hex RGB颜色转换
+ (UIColor *)getHexColor:(NSString *)hexColor
{
unsigned int red,green,blue;
NSRange range;
range.length = 2;
range.location = 0;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];
range.location = 2;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];
range.location = 4;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];
return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green / 255.0f) blue:(float)(blue / 255.0f)];
}

@end

//项目中使用
self.view.backgroundColor = [UIColor getHexColor:@"efeff4" ];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 颜色转换 16进制