您的位置:首页 > 产品设计 > UI/UE

从字符串或16进制数得到UIColor

2015-03-24 11:26 127 查看
+(UIColor*)GetColorFromCSSHex:(NSString *) hexColor{ // @"#FF3300"

if (hexColor == nil || [hexColor isEqualToString:@""]) {

return nil;

}

if ([hexColor length] != 7) {

return nil;

}

//扫描十六进制字符串

unsigned int red = 255, green = 255, blue = 255;

NSRange range;

range.length = 2;

range.location = 1;

[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];//扫描一个十六进制表示的无符号值且取出并转换成十进制存储在red中

range.location = 3;

[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];

range.location = 5;

[[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:1.0f];

}

+(UIColor*)GetColorFromHexnum:(NSInteger)hexColor //0xFF3300

{

Byte red = hexColor>>16;

Byte green = hexColor>>8;

Byte blue = hexColor;

return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green/255.0f) blue:(float)(blue/255.0f) alpha:1.0f];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: