本羣【ken_深圳】的第一次分享
來自Ken的獨白:由於真正接觸IOS就幾個月,也木有什麼存儲箱,就拿以前剛學的是的一個自定義顏色類
.h 文件
#import <UIKit/UIKit.h>
//category for class of UIColor
@interface UIColor(Random)
//create random color
+ (UIColor *)randomColor;//隨機顏色
+ (UIColor *) getColor: (NSString *) hexColor;//獲得顏色
@end
.m文件
#import "UIColor_Random.h"
@implementation UIColor(Random)
//create random color
+ (UIColor *)randomColor {
static BOOL seeded = NO;
if (!seeded) {
seeded = YES;
(time(NULL));
}
CGFloat red = (CGFloat)random() / (CGFloat)RAND_MAX;
CGFloat green = (CGFloat)random() / (CGFloat)RAND_MAX;
CGFloat blue = (CGFloat)random() / (CGFloat)RAND_MAX;
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}
+ (UIColor *) getColor: (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) alpha:0.35f];
}
@end
小編:
真是難爲IOS兄了,不過寫總結對自身是很有好處的,希望以後多多分享。