博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UIColor与十六进制字符串互转
阅读量:6441 次
发布时间:2019-06-23

本文共 4550 字,大约阅读时间需要 15 分钟。

UIColor转十六进制字符串方法一:经过测试发现部分颜色会转化为#fffff(五个f),转化残缺导致最后颜色无法还原,不推荐使用。// UIColor转#ffffff格式的字符串+ (NSString *)hexFromUIColor:(UIColor *)color{    if (CGColorGetNumberOfComponents(color.CGColor) < 4) {        const CGFloat *components = CGColorGetComponents(color.CGColor);        color = [UIColor colorWithRed:components[0]                                green:components[0]                                 blue:components[0]                                alpha:components[1]];    }    if (CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) != kCGColorSpaceModelRGB) {        return [NSString stringWithFormat:@"#FFFFFF"];    }    return [NSString stringWithFormat:@"#%x%x%x", (int)((CGColorGetComponents(color.CGColor))[0]*255.0),            (int)((CGColorGetComponents(color.CGColor))[1]*255.0),            (int)((CGColorGetComponents(color.CGColor))[2]*255.0)];}方法二:完美转化为#ffffff格式,暂未发现问题,推荐使用。// UIColor转#ffffff格式的字符串+ (NSString *)hexStringFromColor:(UIColor *)color {    const CGFloat *components = CGColorGetComponents(color.CGColor);    CGFloat r = components[0];    CGFloat g = components[1];    CGFloat b = components[2];    return [NSString stringWithFormat:@"#%02lX%02lX%02lX",            lroundf(r * 255),            lroundf(g * 255),            lroundf(b * 255)];}十六进制字符串转UIColor方法一:+ (CGFloat) colorComponentFrom: (NSString *) string start: (NSUInteger) start length: (NSUInteger) length {    NSString *substring = [string substringWithRange: NSMakeRange(start, length)];    NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];    unsigned hexComponent;    [[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];    return hexComponent / 255.0;}+ (UIColor *) colorWithHexString: (NSString *) hexString {    NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];    CGFloat alpha, red, blue, green;    switch ([colorString length]) {        case 3: // #RGB            alpha = 1.0f;            red   = [self colorComponentFrom: colorString start: 0 length: 1];            green = [self colorComponentFrom: colorString start: 1 length: 1];            blue  = [self colorComponentFrom: colorString start: 2 length: 1];            break;        case 4: // #ARGB            alpha = [self colorComponentFrom: colorString start: 0 length: 1];            red   = [self colorComponentFrom: colorString start: 1 length: 1];            green = [self colorComponentFrom: colorString start: 2 length: 1];            blue  = [self colorComponentFrom: colorString start: 3 length: 1];            break;        case 6: // #RRGGBB            alpha = 1.0f;            red   = [self colorComponentFrom: colorString start: 0 length: 2];            green = [self colorComponentFrom: colorString start: 2 length: 2];            blue  = [self colorComponentFrom: colorString start: 4 length: 2];            break;        case 8: // #AARRGGBB            alpha = [self colorComponentFrom: colorString start: 0 length: 2];            red   = [self colorComponentFrom: colorString start: 2 length: 2];            green = [self colorComponentFrom: colorString start: 4 length: 2];            blue  = [self colorComponentFrom: colorString start: 6 length: 2];            break;        default:            return nil;    }    return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];}方法二:+ (UIColor *)colorWithHexString:(NSString *)hexString alpha:(CGFloat)alphaValue {    NSString *cString = [[hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];    if (cString.length < 6)    return [UIColor clearColor];    if ([cString hasPrefix:@"0X"])    cString = [cString substringFromIndex:2];    if ([cString hasPrefix:@"#"])    cString = [cString substringFromIndex:1];    if (cString.length != 6)    return [UIColor clearColor];    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];    unsigned int r, g, b;    [[NSScanner scannerWithString:rString] scanHexInt:&r];    [[NSScanner scannerWithString:gString] scanHexInt:&g];    [[NSScanner scannerWithString:bString] scanHexInt:&b];    // 注意:myColor是自定义宏    //#define myColor(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]    return myColor(r, g, b, alphaValue);}

 

转载地址:http://bmdwo.baihongyu.com/

你可能感兴趣的文章
BFS 2015百度之星初赛2 HDOJ 5254 棋盘占领
查看>>
LeetCode 3
查看>>
MySQL自定义函数用法详解-复合结构自定义变量/流程控制
查看>>
活灵活现用 Git --基础篇
查看>>
消息队列mq
查看>>
Linux GIT服务器配置
查看>>
Oracle EBS-SQL (INV-1):检查物料成本为0并且物料状态不是'NEW'的物料.sql
查看>>
文件编码、charset、sublime编辑器支持GBK等问题
查看>>
上传照片预览 js 方法
查看>>
2017-2018-2 20165306 实验五《网络编程与安全》实验报告
查看>>
发送 email 过程
查看>>
关于window的端口查看及tomcat的端口修改问题
查看>>
Google+ Sign-In:联合身份认证、授权和语义活动流
查看>>
横竖连续打印的解决办法
查看>>
Effective Objective-C 2.0 — 第四条:多用类型常量,少用#define预处理指令
查看>>
Effective Objective-C 2.0 — 第8条:理解“对象等同性”这一概念
查看>>
python3 + sublime text3输出中文乱码的问题
查看>>
数据库表结构上下文
查看>>
创建标签的两种方法insertAdjacentHTML 和 createElement 创建标签 setAttribute 赋予标签类型 appendChild 插入标签...
查看>>
Object_C初始化方法, 遍历构造器
查看>>