1. 程式人生 > >十六進位制字串方法獲取顏色

十六進位制字串方法獲取顏色

該方法可直接使用

//給定6位十六進位制字串、獲取十六進位制顏色字串對應的顏色

+ (UIColor *)getColor:(NSString *)hexColor{

//定義無符號整型數值

unsignedint red,green,blue;

NSRange range;

    range.length = 2;

//提取指定下標索引、指定長度的十六進位制字串掃描字串取出的redgreenblue都為 0 ~ 255的整型值

    range.location = 0;

    [[NSScannerscannerWithString:[hexColor substringWithRange

:range]] scanHexInt:&red];

    range.location = 2;

    [[NSScannerscannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];

    range.location = 4;

    [[NSScannerscannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];

//返回通過十六進位制顏色字串獲取的顏色

return

[UIColorcolorWithRed:(float)(red/255.0f) green:(float)(green / 255.0f) blue:(float)(blue / 255.0f) alpha:1.0f];

}