擁有一個 32 位整數 intARGB = 4294967295(即 0xFFFFFFFF),對應 C++ 中的 setRGB() 參數。需在 iOS 中解析出 Alpha、Red、Green、Blue 四個分量並生成 UIColor。
32 位 ARGB 佈局:AAAAAAAA RRRRRRRR GGGGGGGG BBBBBBBB(高位到低位)。提取方法:
(intARGB >> 24) & 0xFF(intARGB >> 16) & 0xFF(intARGB >> 8) & 0xFFintARGB & 0xFFuint32_t intARGB = 4294967295; // 0xFFFFFFFF
CGFloat alpha = ((intARGB >> 24) & 0xFF) / 255.0f;
CGFloat red = ((intARGB >> 16) & 0xFF) / 255.0f;
CGFloat green = ((intARGB >> 8) & 0xFF) / 255.0f;
CGFloat blue = (intARGB & 0xFF) / 255.0f;
UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
// 結果:白色不透明 colorWithRed:1 green:1 blue:1 alpha:1
為 UIColor 添加便利構造器:
// UIColor+Hex.h
@interface UIColor (Hex)
+ (UIColor *)colorWithARGB:(uint32_t)argb;
@end
// UIColor+Hex.m
@implementation UIColor (Hex)
+ (UIColor *)colorWithARGB:(uint32_t)argb {
return [UIColor colorWithRed:((argb >> 16) & 0xFF)/255.0f
green:((argb >> 8) & 0xFF)/255.0f
blue:(argb & 0xFF)/255.0f
alpha:((argb >> 24) & 0xFF)/255.0f];
}
@end
// 使用
UIColor *c = [UIColor colorWithARGB:0xFFFF0000]; // 紅色不透明
uint32_t),避免符號位擴展導致負數錯誤。colorWithRGB:0xFF0000。UIColor 分量範圍 0.0~1.0,除以 255.0 轉換。位運算提取顏色分量是跨平台通用技巧,iOS 中配合 UIColor 構造器即可無縫對接 C++ 數據。