大家好,欢迎来到IT知识分享网。
很多地方我们都使用16进制颜色,但iPhone使用的是UIColor对象,不直接支持16进制颜色,为此,需要我们手动将16进制颜色转换为UIColor。
– (UIColor
{
unsigned int red,green,blue;
NSRange range;
range.length = 2;
range.location = 0;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&red];
range.location = 2;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&green];
range.location = 4;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&blue];
return [UIColor
}
[self.view setBackgroundColor:[self getColor:@”FF0000″]];
一个面试题:使用内联函数把@“#ff3344”转成UIColor
- – (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;
- }
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/106928.html