16进制色值转换UIColor,UILabel自适应宽度
2016-07-27 00:00
309 查看
摘要: 简单封装,16进制色值转换成UIColor,直接使用,以及Label自适应宽度,提高开发。
如:#1e1e1e #ff5f5f转换成UIColor
- (UIColor *) colorWithHexString: (NSString *)color
{
NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6) {
return [UIColor clearColor];
}
// 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 clearColor];
// Separate into r, g, b substrings
NSRange range;
range.location = 0;
range.length = 2;
//r
NSString *rString = [cString substringWithRange:range];
//g
range.location = 2;
NSString *gString = [cString substringWithRange:range];
//b
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];
}
// 计算label的自适应宽度
- (CGFloat)getTextWidthForString:(NSString *)string with:(CGFloat)height with:(UIFont *)font{
CGRect rect = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
return rect.size.width;
}
如:#1e1e1e #ff5f5f转换成UIColor
- (UIColor *) colorWithHexString: (NSString *)color
{
NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6) {
return [UIColor clearColor];
}
// 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 clearColor];
// Separate into r, g, b substrings
NSRange range;
range.location = 0;
range.length = 2;
//r
NSString *rString = [cString substringWithRange:range];
//g
range.location = 2;
NSString *gString = [cString substringWithRange:range];
//b
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];
}
// 计算label的自适应宽度
- (CGFloat)getTextWidthForString:(NSString *)string with:(CGFloat)height with:(UIFont *)font{
CGRect rect = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
return rect.size.width;
}
RGB 色值处理 定义成宏 #define RGBColorMake(_R_,_G_,_B_,_alpha_) [UIColor colorWithRed:_R_/255.0 green:_G_/255.0 blue:_B_/255.0 alpha:_alpha_]
相关文章推荐
- UILabel的高度和宽度自适应
- 网页富文本编辑器の小分享
- IBM的蓝色蔓延式创新,用Bluemix“混搭”出个云生态
- UISlider遇到的问题和解决
- ligerui 表格缩放自适应
- Builder模式
- Multiple sequence alignment Benchmark Data set
- HDU 2818 Building Block
- 动态规划之前缀动态规划:poj 1458 Common Subsequence(最长公共子序列)
- [置顶] Android DocumentUi(下载管理)是如何向DownloadProvider中获取数据的
- hdu 3989 HP and Polyjuice Potion & hdu 4626 Jinkeloid
- UI基础_自定义非等高Cell_Xib封装_版本_有GIF_源代码
- UISlider无法拖动进度条的问题解决
- UI基础_自定义非等高Cell_storyboard封装_版本_有GIF_源代码
- UEFI引导在GPT分区下安装win2008——抓住那只傲娇的win2008
- Codeforces 612C Replace To Make Regular Bracket Sequence【栈】
- easyui源码分析(parser.js)(3)
- Confluent介绍(二)--confluent platform quickstart
- lintcode unique-paths-ii 不同的路径ii
- HDOJ 1242 Rescue(BFS + 优先队列)