开发中 十六进制的颜色值直接转为ios可用的UIColor
2016-07-03 22:50
363 查看
用法很简单,直接创建一个UIColor的扩展文件,添加对应的方法,在代码中需要用到设置颜色那块,直接调用该方法即可
@interface UIColor (extension)
+ (UIColor*) colorWithHex:(NSInteger)hexValue alpha:(CGFloat)alphaValue;
+ (UIColor*) colorWithHex:(NSInteger)hexValue;
+ (NSString *) hexFromUIColor: (UIColor*) color;
@end
@implementation UIColor (extension)
+ (UIColor*) colorWithHex:(NSInteger)hexValue alpha:(CGFloat)alphaValue
{
return [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0
green:((float)((hexValue & 0xFF00) >> 8))/255.0
blue:((float)(hexValue & 0xFF))/255.0 alpha:alphaValue];
}
+ (UIColor*) colorWithHex:(NSInteger)hexValue
{
return [UIColor colorWithHex:hexValue alpha:1.0];
}
+ (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)];
}
@end
@interface UIColor (extension)
+ (UIColor*) colorWithHex:(NSInteger)hexValue alpha:(CGFloat)alphaValue;
+ (UIColor*) colorWithHex:(NSInteger)hexValue;
+ (NSString *) hexFromUIColor: (UIColor*) color;
@end
@implementation UIColor (extension)
+ (UIColor*) colorWithHex:(NSInteger)hexValue alpha:(CGFloat)alphaValue
{
return [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0
green:((float)((hexValue & 0xFF00) >> 8))/255.0
blue:((float)(hexValue & 0xFF))/255.0 alpha:alphaValue];
}
+ (UIColor*) colorWithHex:(NSInteger)hexValue
{
return [UIColor colorWithHex:hexValue alpha:1.0];
}
+ (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)];
}
@end
相关文章推荐
- 十六进制颜色的设定
- 二进制颜色代码大全(含图)透明度与十六进制代码转换
- 十六进制颜色
- 十六进制颜色值和ARGB颜色值的转换
- iOS 一句代码轻松实现十六进制颜色(#开头)转RGB颜色
- 通过闭包和setTimeout让背景颜色从黄变白再变紫红
- JS颜色十六进制转换为rgb或rgba
- Android系统应用---SystemUI之一:SystemUI概述和创建启动流程分析
- 关于UITableView中删除某一行方法的使用
- 修改UITextField的placeholder颜色
- UIStepper
- easyui radio 无法选中的原因
- 使用easyUI遇到的一些小问题
- POJ2299 Ultra-QuickSort
- LeetCode - 111. Minimum Depth of Binary Tree
- UIPageControl
- Deep Reinforcement Learning for Dialogue Generation-关于生成对话的深度强化学习
- Android UI 设计 练习1 ——登陆界面
- LeetCode 300 Longest Increasing Subsequence
- PP66 EEPPPPMM SSyysstteemm AAddmmiinniissttrraattiioonn GGuuiiddee 16 R1