在安卓系统中,有一种选项可以根据int值设置颜色。那么我能否使用这个值或将其转换为iOS系统下的UIColor
来设置呢?比如说,像这个值,-5242884
,能够在安卓系统中被用于设置颜色。
据我所知,在iOS系统中,广泛使用十六进制值或RGB值来设置UIColor
。但是我没能找到任何关于解决我的问题的信息。是否有什么方法可以使用int值来代替呢?
在安卓系统中,有一种选项可以根据int值设置颜色。那么我能否使用这个值或将其转换为iOS系统下的UIColor
来设置呢?比如说,像这个值,-5242884
,能够在安卓系统中被用于设置颜色。
据我所知,在iOS系统中,广泛使用十六进制值或RGB值来设置UIColor
。但是我没能找到任何关于解决我的问题的信息。是否有什么方法可以使用int值来代替呢?
看起来你引用的整数是打包的整数。参见:Android 颜色文档。你需要找到一种将打包的整型转换为十六进制的方法,然后可以使用此宏(从此处获取),将十六进制数转换为 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]
我知道这并没有真正回答你的问题,但它可能会帮助你缩小搜索范围。
编辑
好的,我刚刚意识到上面的宏确实解决了你的问题。只需给它整数表示,它就会给你正确的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
我从这里得到了一些帮助。
编辑
修正了宏以期望正确的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]
之前的宏期望的是RGBA格式,而安卓颜色整数提供的是ARGB格式。
color
对象在Android中显示为蓝色,但在iOS中显示为红色。不幸的是,宏的准确性有所欠缺。 - IssacZH./**
An extension to `UIColor` that provides convenience methods for working with Android packed ColorInt values.
*/
extension UIColor {
/**
Initializes a `UIColor` instance with the specified Android packed ColorInt value.
- Parameter colorInt: The Android packed ColorInt value.
- Returns: A new `UIColor` instance.
- Note: The `colorInt` value should be a 32-bit integer representing a color in ARGB format.
*/
convenience init(colorInt: Int32) {
let alpha = (colorInt >> 24) & 0xFF
let red = (colorInt >> 16) & 0xFF
let green = (colorInt >> 8) & 0xFF
let blue = colorInt & 0xFF
self.init(red: CGFloat(red) / 255.0,
green: CGFloat(green) / 255.0,
blue: CGFloat(blue) / 255.0,
alpha: CGFloat(alpha) / 255.0)
}
/**
Retrieves the Android packed ColorInt value of the `UIColor` instance.
- Returns: The Android packed ColorInt value.
- Note: The returned value is a 32-bit integer representing the color in ARGB format.
*/
var colorInt: Int32 {
let components = cgColor.components ?? [0, 0, 0, 0]
let red = components[0] * 255
let green = components[1] * 255
let blue = components[2] * 255
let alpha = components[3] * 255
return Int32(alpha) << 24 | Int32(red) << 16 | Int32(green) << 8 | Int32(blue)
}
}
let packedColor: Int32 = -16776961
let color = UIColor(colorInt: packedColor)
print(color) // Output: UIExtendedSRGBColorSpace 0 0 1 1
let extractedColorInt = color.colorInt
print(extractedColorInt) // Output: -16776961
print(UIColor.white.colorInt)
。并非所有颜色都是RGBA,并且并非所有颜色都有4个组件。 - HangarRash如果有帮助的话,这是 Swift 的版本:
func getUIColorFromAndroidColorInt(androidColorInt: Int) -> UIColor {
let red = (CGFloat) ( (androidColorInt>>16)&0xFF )
let green = (CGFloat) ( (androidColorInt>>8)&0xFF )
let blue = (CGFloat) ( (androidColorInt)&0xFF )
return UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1)
}
view.setBackgroundColor(color)
将color
定义为上述的值。 - IssacZH.