美文网首页
iOS 16进制字符串转颜色

iOS 16进制字符串转颜色

作者: iOS程序媛ff | 来源:发表于2017-07-14 11:38 被阅读140次

通常UI给我们的色值都是16进制,我们需要进行转换

#import@interface UIColor (NSString)

/**

*  16进制字符串转颜色

*

*  @param hexColor 16进制字符串

*

*  @return 返回颜色

*/

+ (UIColor *)getColor:(NSString *)hexColor;

@end

实现:

#pragma -mark GetColor//解析16进制的颜色

+ (UIColor *)getColor:(NSString *)hexColor {

unsigned int red,green,blue;

NSRange range;

range.length = 2;

range.location = 0;

[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];

range.location = 2;

[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];

range.location = 4;

[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];

return RGBACOLOR(red, green, blue, 1.0);

}

相关文章

网友评论

      本文标题:iOS 16进制字符串转颜色

      本文链接:https://www.haomeiwen.com/subject/lfouzttx.html