Iss*_*ZH. 2 hex objective-c uicolor ios
在android中有一个选项,你可以根据int值设置颜色.无论如何,我可以使用此值或转换此值来设置它UIColor在iOS中?例如这个值,-5242884可以在android中用来设置颜色.
从我设法找到的是iOS广泛使用十六进制值或rgb值来设置UIColor.但我在这里找不到有关我的问题的任何信息.无论如何,我可以使用int值代替.
nal*_*d88 10
看来您引用的整数是一个压缩整数.请参阅:android颜色文档.您需要找到一种方法将packed int转换为十六进制,然后您可以使用此宏(此处获取)将十六进制数转换为UIColor:
#define HEXCOLOR(c) [UIColor colorWithRed:((c>>24)&0xFF)/255.0 green:((c>>16)&0xFF)/255.0 blue:((c>>8)&0xFF)/255.0 alpha:((c)&0xFF)/255.0]
Run Code Online (Sandbox Code Playgroud)
我知道这并不能真正回答你的问题,但它可能会帮助你缩小搜索范围.
编辑
好的,我刚才意识到上面的宏确实解决了你的问题.只需给它整数表示,它将为您提供正确的UIColor.我的测试代码如下:
UIColor *color = HEXCOLOR(-16776961); // Blue const from android link above
const CGFloat *components = CGColorGetComponents(color.CGColor);
NSString *colorAsString = [NSString stringWithFormat:@"%f,%f,%f,%f", components[0], components[1], components[2], components[3]];
NSLog(@"%@",colorAsString); // Prints 1.0 0.0 0.0 1.0 which corresponds to 0xff0000ff
Run Code Online (Sandbox Code Playgroud)
我从这里得到了一些帮助.
编辑
修复了宏以期望正确的RGBA值:
#define ANDROID_COLOR(c) [UIColor colorWithRed:((c>>16)&0xFF)/255.0 green:((c>>8)&0xFF)/255.0 blue:((c)&0xFF)/255.0 alpha:((c>>24)&0xFF)/255.0]
Run Code Online (Sandbox Code Playgroud)
之前的宏预期RGBA,而Android颜色int给了ARGB.
| 归档时间: |
|
| 查看次数: |
3933 次 |
| 最近记录: |