十六进制颜色转换为iOS可以用的UIColor
2015-10-29 19:46
429 查看
//
// UIColor+Transformation.h
// ContactApp
//
// Created by 袁冬冬 on 15/9/11.
// Copyright (c) 2015年 . All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIColor (Transformation)
+ (UIColor *)transformat:(NSString *)colorString;
@end
//
// UIColor+Transformation.m
// ContactApp
//
// Created by 袁冬冬 on 15/9/11.
// Copyright (c) 2015年 . All rights reserved.
//
#import "UIColor+Transformation.h"
@implementation UIColor (Transformation)
+ (UIColor *)transformat:(NSString *)colorString {
//去掉十六进制的#例如(#FFFFFF)
NSString *newColorString = [colorString substringFromIndex:1];
NSRange r1 = {0,2};
NSRange r2 = {2,2};
NSRange r3 = {4,2};
NSString *redStr = [newColorString substringWithRange:r1];
NSString *greenStr = [newColorString substringWithRange:r2];
NSString *blueStr = [newColorString substringWithRange:r3];
ColorBlock([redStr substringToIndex:1]);
int red = [ColorBlock([redStr substringToIndex:1]) intValue] * 16 + [ColorBlock([redStr substringFromIndex:1]) intValue];
int green = [ColorBlock([greenStr substringToIndex:1]) intValue] * 16 + [ColorBlock([greenStr substringFromIndex:1]) intValue];
int blue = [ColorBlock([blueStr substringToIndex:1]) intValue] * 16 + [ColorBlock([blueStr substringFromIndex:1]) intValue];
float r = red / 255.0;
float g = green / 255.0;
float b = blue / 255.0;
UIColor *c = [UIColor colorWithRed:r green:g blue:b alpha:1.0];
return c;
}
NSString * (^ColorBlock)(NSString *) = ^(NSString *str) {
if([str isEqualToString:@"A"]){
str = @"10";
}else if ([str isEqualToString:@"B"]){
str = @"11";
}else if ([str isEqualToString:@"C"]){
str = @"12";
}
else if ([str isEqualToString:@"D"]){
str = @"13";
}
else if ([str isEqualToString:@"E"]){
str = @"14";
}else if ([str isEqualToString:@"F"]){
str = @"15";
}
return str;
};
@end
// UIColor+Transformation.h
// ContactApp
//
// Created by 袁冬冬 on 15/9/11.
// Copyright (c) 2015年 . All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIColor (Transformation)
+ (UIColor *)transformat:(NSString *)colorString;
@end
//
// UIColor+Transformation.m
// ContactApp
//
// Created by 袁冬冬 on 15/9/11.
// Copyright (c) 2015年 . All rights reserved.
//
#import "UIColor+Transformation.h"
@implementation UIColor (Transformation)
+ (UIColor *)transformat:(NSString *)colorString {
//去掉十六进制的#例如(#FFFFFF)
NSString *newColorString = [colorString substringFromIndex:1];
NSRange r1 = {0,2};
NSRange r2 = {2,2};
NSRange r3 = {4,2};
NSString *redStr = [newColorString substringWithRange:r1];
NSString *greenStr = [newColorString substringWithRange:r2];
NSString *blueStr = [newColorString substringWithRange:r3];
ColorBlock([redStr substringToIndex:1]);
int red = [ColorBlock([redStr substringToIndex:1]) intValue] * 16 + [ColorBlock([redStr substringFromIndex:1]) intValue];
int green = [ColorBlock([greenStr substringToIndex:1]) intValue] * 16 + [ColorBlock([greenStr substringFromIndex:1]) intValue];
int blue = [ColorBlock([blueStr substringToIndex:1]) intValue] * 16 + [ColorBlock([blueStr substringFromIndex:1]) intValue];
float r = red / 255.0;
float g = green / 255.0;
float b = blue / 255.0;
UIColor *c = [UIColor colorWithRed:r green:g blue:b alpha:1.0];
return c;
}
NSString * (^ColorBlock)(NSString *) = ^(NSString *str) {
if([str isEqualToString:@"A"]){
str = @"10";
}else if ([str isEqualToString:@"B"]){
str = @"11";
}else if ([str isEqualToString:@"C"]){
str = @"12";
}
else if ([str isEqualToString:@"D"]){
str = @"13";
}
else if ([str isEqualToString:@"E"]){
str = @"14";
}else if ([str isEqualToString:@"F"]){
str = @"15";
}
return str;
};
@end
相关文章推荐
- MySQL Row size too large (> 8126) 以及ROW_FORMAT=COMPRESSED requires innodb_file_format > Antel 的解决办法
- LeetCode Unique Binary Search Trees (DP)
- UIDevice
- 翻译:Fast Exact Shortest-Path Distance Queries on LargeNetworks by Pruned Landmark Labeling
- Warning: Attempt to present <UIImagePickerController: 0x292b400> on xxx which is already presenti
- [转]Android UI 自动化测试
- android-消息传递机制Message、MessageQueue、Handler、Looper
- hdu 5504 GT and sequence
- 关于UIInterfaceOritation 和 UIDeviceOritation
- 关于mysql的错误 - no query specified
- Android - Get Bluetooth UUID for this device
- iOS UI设计: 在Autolayout的布局上用constraint实现动画
- iOS流布局UICollectionView系列七——三维中的球型布局
- Requests高级用法
- Java - String vs StringBuffer vs StringBuilder
- 【解决方法】You must rebuild it with bitcode enabled (Xc
- Golang的循环中break和continue语句的用法讲解
- 杭电1242Rescue(深搜)
- uestc 1221 Ancient Go
- UIViewController的生命周期