我想从十六进制值创建 UIColor。但我的颜色是 NSString。所以我实现了这个解决方案: How can I create a UIColor from a hex string?
代码:
#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
然后我有(简化):
NSString *myString = [[NSString alloc] initWithString:@"0xFFFFFF"];
所以当我想调用宏时:
UIColor *myColor = UIColorFromRGB(myString);
我收到一个错误:二进制表达式的无效操作数('NSString *' 和 'int')
所以我知道我必须传递 int,但是在这种情况下如何将 NSString 转换为 int?当然 [myString intValue];
在这里不起作用。
最佳答案
您可以像这样直接使用您的宏和十六进制整数:
UIColorFromRGB(0xff4433)
或者您可以像这样将十六进制字符串解析为整数:
unsigned colorInt = 0;
[[NSScanner scannerWithString:hexString] scanHexInt:&colorInt];
UIColorFromRGB(colorInt)
关于iphone - Objective-C 中的十六进制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10297302/