iOS 开发使用16进制色值设置颜色

来源:互联网 发布:网上商城美工招聘 编辑:程序博客网 时间:2024/05/29 19:41

具体实现如下:

通过对象调用下面方法,传入色值字符串,返回UIColor对象为title的textColor设置颜色>>

//传入的色值为:#3f51b5self.title.textColor = [selfcolorFromHexRGB:@"3f51b5"];​//转换16进制色值为UIColor对象- (UIColor *) colorFromHexRGB:(NSString *) inColorString{    UIColor *result = nil;    unsigned int colorCode = 0;    unsigned char redByte, greenByte, blueByte;        if (nil != inColorString)    {        NSScanner *scanner = [NSScanner scannerWithString:inColorString];        (void) [scanner scanHexInt:&colorCode]; // ignore error    }    redByte = (unsigned char) (colorCode >> 16);    greenByte = (unsigned char) (colorCode >> 8);    blueByte = (unsigned char) (colorCode); // masks off high bits    result = [UIColor              colorWithRed: (float)redByte / 0xff              green: (float)greenByte/ 0xff              blue: (float)blueByte / 0xff              alpha:1.0];    return result;}


0 0