1.建立一个UIColor的category,命名为UIColor+Helper
然后在UIColor+Helper.h中:
//16进制字符串转换成UIColor,返回UIColor
+ (UIColor *)colorWithHexStr:(NSString *)colorStr;
在UIColor+Helper.m中:
//16进制字符串转换成UIColor
+ (UIColor *)colorWithHexStr:(NSString *)colorStr{
NSString * redStr = [colorStr substringWithRange:NSMakeRange(0, 2)];
NSScanner * redScanner = [NSScanner scannerWithString:redStr];
unsigned int redIntValue;
[redScanner scanHexInt:&redIntValue];
NSString * greenStr = [colorStr substringWithRange:NSMakeRange(2, 2)];
NSScanner * greenScanner = [NSScanner scannerWithString:greenStr];
unsigned int greenIntValue;
[greenScanner scanHexInt:&greenIntValue];
NSString * blueStr = [colorStr substringWithRange:NSMakeRange(4, 2)];
NSScanner * blueScanner = [NSScanner scannerWithString:blueStr];
unsigned int blueIntValue;
[blueScanner scanHexInt:&blueIntValue];
return [UIColor colorWithRed:redIntValue/255.0 green:greenIntValue/255.0 blue:blueIntValue/255.0 alpha:1];
}
多数用于服务器后端传值,然后前端将16进制的色值展示出来
本文介绍了一个用于将16进制颜色值转换为UIColor的Objective-C类别方法。通过创建UIColor+Helper分类,实现了从服务器接收16进制颜色字符串,并在iOS应用中将其转换为UIColor的功能。
1094

被折叠的 条评论
为什么被折叠?



