iOS 十六进制颜色字符串转为UIColor —— HERO博客
2016-01-26 22:40
656 查看
iOS 十六进制颜色字符串转为UIColor ,下面贴上代码:
#import <UIKit/UIKit.h>
@interface UIColor (PN)
/**
* 16进制转化RGB
*/
+ (UIColor *)colorWithHexString:(NSString *)string;
/**
* 随即色
*/
+ (UIColor *)randomColor;
@end
#import "UIColor+PN.h"
@implementation UIColor (PN)
+ (UIColor *)colorWithHexString:(NSString *)string
{
if ([string hasPrefix:@"#"])
string = [string substringFromIndex:1];
// Separate into r, g, b substrings
NSRange range;
range.length = 2;
range.location = 0;
NSString *rString = [string substringWithRange:range];
range.location = 2;
NSString *gString = [string substringWithRange:range];
range.location = 4;
NSString *bString = [string substringWithRange:range];
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:1];
}
+ (UIColor *)randomColor
{
CGFloat red = arc4random_uniform(256);
CGFloat green = arc4random_uniform(256);
CGFloat blue = arc4random_uniform(256);
return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1];
}
@end
#import <UIKit/UIKit.h>
@interface UIColor (PN)
/**
* 16进制转化RGB
*/
+ (UIColor *)colorWithHexString:(NSString *)string;
/**
* 随即色
*/
+ (UIColor *)randomColor;
@end
#import "UIColor+PN.h"
@implementation UIColor (PN)
+ (UIColor *)colorWithHexString:(NSString *)string
{
if ([string hasPrefix:@"#"])
string = [string substringFromIndex:1];
// Separate into r, g, b substrings
NSRange range;
range.length = 2;
range.location = 0;
NSString *rString = [string substringWithRange:range];
range.location = 2;
NSString *gString = [string substringWithRange:range];
range.location = 4;
NSString *bString = [string substringWithRange:range];
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:1];
}
+ (UIColor *)randomColor
{
CGFloat red = arc4random_uniform(256);
CGFloat green = arc4random_uniform(256);
CGFloat blue = arc4random_uniform(256);
return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1];
}
@end
相关文章推荐
- iOS9.2崩溃:NSTimer(PhotosUI) blockInvoke
- Spark问题之More than one scala library found in the build path
- NGUI移动平台屏幕自适应问题
- UUIDUtils
- NYOJ--1100--WAJUEJI which home strong!
- uuid.js
- NGUI 中用代码给UILable中的字符串上色
- 94. Unique Binary Search Trees
- UUID全球唯一的ID
- UISegmentedControl的基本用法
- 303. Range Sum Query - Immutable
- UIAlertController的使用——创建提示窗口
- iOS主线程更新UI
- UI父视图子视图加载
- Packet for query is too large
- Dynamic Web Module 3.0 requires Java 1.6 or newer
- MR-2.输入格式(InputFormat)TextInputFormat和SequenceFileInputFormat源码分析
- android 不能在子线程中更新ui的讨论和分析
- UE4 main classes explanation
- UI_UIImageView 基本操作