UIColor

总结几个UIColor 的技巧:

/**
 @brief 根据r,g,b的值(0~255)创建UIColor
 */
#define UIColorWithRGB(r, g, b) [UIColor colorWithRed:(r) / 255.0f green:(g) / 255.0f blue:(b) / 255.0f alpha:1.0f]

/**
 @brief 根据r,g,b,a的值(0~255)创建UIColor
 */
#define UIColorWithRGBA(r, g, b, a) [UIColor colorWithRed:(r) / 255.0f green:(g) / 255.0f blue:(b) / 255.0f alpha:(a) / 255.0f]

/**
 @brief 根据十六进制创建UIColor
 */
#define UIColorFromHex(s) [UIColor colorWithRed:(((s & 0xFF0000) >> 16))/255.0green:(((s &0xFF00) >>8))/255.0blue:((s &0xFF))/255.0alpha:1.0]


通过这个程序就能明白:

- (UIColor *) stringTOColor:(NSString *)str  
{  
    if (!str || [str isEqualToString:@""]) {  
        return nil;  
    }  
    unsigned red,green,blue;  
    NSRange range;  
    range.length = 2;  
    range.location = 1;  
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&red];  
    range.location = 3;  
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&green];  
    range.location = 5;  
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&blue];  
    UIColor *color= [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1];  
    return color;  
}  

其实RGB就是每两位的十六进制来分别表示,很明显,UIColor的十六进制和RGB其实就是一回事,计算机科学也非常强大,能用数理去统一它



参考:

http://blog.csdn.net/youcanping2008/article/details/9207431

http://www.jianshu.com/p/d3fd0862e843


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值