iOS 设置#ffff 这种颜色
2024-10-15 20:21:29
UI给图的时候给的是#f2f2f2 让我设置。没有你要的rgb。 所以只能自行解决封装了代码
HexColors.h
#import "TargetConditionals.h" #if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR #import <UIKit/UIKit.h> #define HXColor UIColor #else #import <Cocoa/Cocoa.h> #define HXColor NSColor #endif @interface HXColor (HexColorAddition) + (HXColor *)hx_colorWithHexString:(NSString *)hexString; + (HXColor *)hx_colorWithHexString:(NSString *)hexString alpha:(CGFloat)alpha; + (HXColor *)hx_colorWith8BitRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue; + (HXColor *)hx_colorWith8BitRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha; @end
HexColors.m
#import "HexColors.h" @implementation HXColor (HexColorAddition) + (HXColor *)hx_colorWithHexString:(NSString *)hexString { // Check for hash and add the missing hash ]) { hexString = [NSString stringWithFormat:@"#%@", hexString]; } CGFloat alpha = 1.0; == hexString.length || == hexString.length) { NSString * alphaHex = [hexString substringWithRange:NSMakeRange(, == hexString.length ? : )]; == alphaHex.length) alphaHex = [NSString stringWithFormat:@"%@%@", alphaHex, alphaHex]; hexString = [NSString stringWithFormat: == hexString.length ? : ]]; unsigned alpha_u = [[self class] hx_hexValueToUnsigned:alphaHex]; alpha = ((CGFloat) alpha_u) / 255.0; } return [[self class] hx_colorWithHexString:hexString alpha:alpha]; } + (HXColor *)hx_colorWithHexString:(NSString *)hexString alpha:(CGFloat)alpha { ) { return nil; } // Check for hash and add the missing hash ]) { hexString = [NSString stringWithFormat:@"#%@", hexString]; } // check for string length != hexString.length && != hexString.length) { NSString *defaultHex = [NSString stringWithFormat:@"0xff"]; unsigned defaultInt = [[self class] hx_hexValueToUnsigned:defaultHex]; HXColor *color = [HXColor hx_colorWith8BitRed:defaultInt green:defaultInt blue:defaultInt alpha:1.0]; return color; } // check for 3 character HexStrings hexString = [[self class] hx_hexStringTransformFromThreeCharacters:hexString]; NSString *redHex = [NSString stringWithFormat:, )]]; unsigned redInt = [[self class] hx_hexValueToUnsigned:redHex]; NSString *greenHex = [NSString stringWithFormat:, )]]; unsigned greenInt = [[self class] hx_hexValueToUnsigned:greenHex]; NSString *blueHex = [NSString stringWithFormat:, )]]; unsigned blueInt = [[self class] hx_hexValueToUnsigned:blueHex]; HXColor *color = [HXColor hx_colorWith8BitRed:redInt green:greenInt blue:blueInt alpha:alpha]; return color; } + (HXColor *)hx_colorWith8BitRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue { return [[self class] hx_colorWith8BitRed:red green:green blue:blue alpha:1.0]; } + (HXColor *)hx_colorWith8BitRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha { HXColor *color = nil; #if (TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE) color = [HXColor colorWithRed:( green:( blue:( alpha:alpha]; #else color = [HXColor colorWithCalibratedRed:( green:( blue:( alpha:alpha]; #endif return color; } + (NSString *)hx_hexStringTransformFromThreeCharacters:(NSString *)hexString { ) { hexString = [NSString stringWithFormat:@"#%1$c%1$c%2$c%2$c%3$c%3$c", [hexString characterAtIndex:], [hexString characterAtIndex:], [hexString characterAtIndex:]]; } return hexString; } + (unsigned)hx_hexValueToUnsigned:(NSString *)hexValue { unsigned value = ; NSScanner *hexValueScanner = [NSScanner scannerWithString:hexValue]; [hexValueScanner scanHexInt:&value]; return value; } @end
调用
self.backgroundColor = [HXColor hx_colorWithHexString:@"ff5a60"];
最新文章
- Angularjs ng-if和ng-show的区别
- 统一配置管理-百度disconf
- C++纯虚函数
- Unix/Linux环境C编程入门教程(10) SUSE Linux EnterpriseCCPP开发环境搭建
- PHP版本替换, phpinfo和php -v显示版本信息不一致
- EF框架搭建小总结--ModelFirst模型优先
- ELK系列~Nxlog日志收集加转发(解决log4日志换行导致json转换失败问题)
- goDaddy SSL证书 Nginx配置全流程 (转)
- Linux安装mysql(Redhat6.5+MySQL5.7)(转载+原创补漏)
- 开机出现loading (hd0)/ntldr。。。
- [Educational Round 17][Codeforces 762F. Tree nesting]
- CSS3 阴影模拟灯照效果
- PostgreSQL 问题总结
- define和typedef的区别
- Win10 1803 Spring Creators update Consumer edition的版本记录
- Redis分布式锁服务(转)
- RSA读取密钥——使用openssl编程
- [Node.js] process.nextTick for converting sync to async
- Hadoop JobTracker和NameNode运行时参数查看
- Kafka: Connect
热门文章
- CodeForces 622C Not Equal on a Segment
- HUST 1376 Random intersection
- 13、手把手教你Extjs5(十三)模块字段和Grid列的定义[1]
- 升级apache
- 关于css中的border
- mybatis sql中if判断传入Integer类型,传入0时,判断没有执行
- 命名空间“Microsoft.AspNet”中不存在类型或命名空间名“Mvc”
- 【Xilinx-Petalinux学习】-06-OpenCV通过USB摄像头采集图像。
- 解開32位元Win 7記憶體4GB限制
- Xcache和memcache的比较