UIColor各种颜色转换

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIColor各种颜色转换相关的知识,希望对你有一定的参考价值。

1、Hex值颜色转换

#import <UIKit/UIKit.h>

@interface UIColor (Extension)

// 根据无符号的32位整数转换为对应的RGB颜色
+ (instancetype)sy_colorWithHex:(u_int32_t)hex;

@end


#import "UIColor+Extension.h"

@implementation UIColor (Extension)

+ (instancetype)sy_colorWithHex:(u_int32_t)hex{

    int red = (hex & 0xFF0000) >> 16;
    int green = (hex & 0x00FF00) >> 8;
    int blue = hex & 0x0000FF;
    
    return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
}

@end

2、将UIColor转换为RGB值

 

- (NSMutableArray *) changeUIColorToRGB:(UIColor *)color  
{  
    NSMutableArray *RGBStrValueArr = [[NSMutableArray alloc] init];  
    NSString *RGBStr = nil;  

    //获得RGB值描述  
    NSString *RGBValue = [NSString stringWithFormat:@"%@",color];  

    //将RGB值描述分隔成字符串  
    NSArray *RGBArr = [RGBValue componentsSeparatedByString:@" "];
  
    //获取红色值  
    int r = [[RGBArr objectAtIndex:1] intValue] * 255;  
    RGBStr = [NSString stringWithFormat:@"%d",r];  
    [RGBStrValueArr addObject:RGBStr];  

    //获取绿色值  
    int g = [[RGBArr objectAtIndex:2] intValue] * 255;  
    RGBStr = [NSString stringWithFormat:@"%d",g];  
    [RGBStrValueArr addObject:RGBStr];  

    //获取蓝色值  
    int b = [[RGBArr objectAtIndex:3] intValue] * 255;  
    RGBStr = [NSString stringWithFormat:@"%d",b];  
    [RGBStrValueArr addObject:RGBStr];  

    //返回保存RGB值的数组  
    return [RGBStrValueArr autorelease];  
}

3、16进制颜色(html颜色值)字符串转为UIColor

+(UIColor *) hexStringToColor: (NSString *) stringToConvert  
{  
     NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];  
     // String should be 6 or 8 characters  
     
     if ([cString length] < 6) return [UIColor blackColor];  
     // strip 0X if it appears  
     if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];  
     if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];  
     if ([cString length] != 6) return [UIColor blackColor];  
     
     // Separate into r, g, b substrings  
     
     NSRange range;  
     range.location = 0;  
     range.length = 2;  
     NSString *rString = [cString substringWithRange:range];  
     range.location = 2;  
     NSString *gString = [cString substringWithRange:range];  
     range.location = 4;  
     NSString *bString = [cString substringWithRange:range];  
     // Scan values  
     unsigned int r, g, b;  
     
     [[NSScanner scannerWithString:rString] scanHexInt:&r];  
     [[NSScanner scannerWithString:gString] scanHexInt:&g];  
     [[NSScanner scannerWithString:bString] scanHexInt:&b];  
     
     return [UIColor colorWithRed:((float) r / 255.0f)  
                                 green:((float) g / 255.0f)  
                                   blue:((float) b / 255.0f)  
                                 alpha:1.0f];  
}

 

以上是关于UIColor各种颜色转换的主要内容,如果未能解决你的问题,请参考以下文章

[iOS]把16进制(#871f78)颜色转换UIColor

[iOS]把16进制(#871f78)颜色转换UIColor

UIColor支持16进制颜色设置

未为 UIColor 定义哈希 UIPlaceholderColor 需要先转换颜色空间

iOS 随机颜色和十六进制颜色转换

如何正确地从 RGB 值初始化 UIColor?