颜色转换:#hhhfff->UIColor (MHHexColoring)
2015-07-14 17:30
537 查看
MHHexColoring为开发者快速获取想要的十六进制颜色(Hex Color)
查找16进制色码的网站:http://www.color-hex.com
// 版权属于原作者 MHHexColoringhttp://code4app.com/ios/MHHexColoring/548e9485933bf0a9738b6301
1、使用方法:
加入UIColor+HexString.h/m文件,导入头文件:
#import "UIColor+HexString.h"
获取颜色,返回UIColor:
[UIColor colorWithHexString:@"#ffffff"];
2、 UIColor+HexString.h
// UIColor+HexString.h
// shopbox
//
// Created by Mohamed Hegab on 10/2/14.
// Copyright (c) 2014 The Dark Dimension. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIColor (HexString)
+ (UIColor *) colorWithHexString: (NSString *) hexString;
@end
3、 UIColor+HexString.m
#import "UIColor+HexString.h"
@implementation UIColor (HexString)
+ (CGFloat) colorComponentFrom: (NSString *) string start: (NSUInteger) start length: (NSUInteger) length {
NSString *substring = [string substringWithRange: NSMakeRange(start, length)];
NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
unsigned hexComponent;
[[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
return hexComponent / 255.0;
}
+ (UIColor *) colorWithHexString: (NSString *) hexString {
NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];
CGFloat alpha, red, blue, green;
switch ([colorString length]) {
case 3: // #RGB
alpha = 1.0f;
red = [self colorComponentFrom: colorString start: 0 length: 1];
green = [self colorComponentFrom: colorString start: 1 length: 1];
blue = [self colorComponentFrom: colorString start: 2 length: 1];
break;
case 4: // #ARGB
alpha = [self colorComponentFrom: colorString start: 0 length: 1];
red = [self colorComponentFrom: colorString start: 1 length: 1];
green = [self colorComponentFrom: colorString start: 2 length: 1];
blue = [self colorComponentFrom: colorString start: 3 length: 1];
break;
case 6: // #RRGGBB
alpha = 1.0f;
red = [self colorComponentFrom: colorString start: 0 length: 2];
green = [self colorComponentFrom: colorString start: 2 length: 2];
blue = [self colorComponentFrom: colorString start: 4 length: 2];
break;
case 8: // #AARRGGBB
alpha = [self colorComponentFrom: colorString start: 0 length: 2];
red = [self colorComponentFrom: colorString start: 2 length: 2];
green = [self colorComponentFrom: colorString start: 4 length: 2];
blue = [self colorComponentFrom: colorString start: 6 length: 2];
break;
default:
return nil;
}
return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
}
@end
相关文章推荐
- MySQL查询报错 ERROR: No query specified
- Druid validationQuery 错误
- BFKit:对常用 UIButton,UIColor,UIDevice,UIFont ,UIImage 等开发类进行了扩展
- JAVA关于Stirng,StringBuilder,StringBuffer的比较
- Android 解决attempt to acquire a reference on a close SQLiteClosable
- Android 解决Android requires compiler compliance level 5.0 or 6.0. Found '1.7'
- UI4_UITableViewSectionIndex
- UI3_UITableViewDelete(多选)
- UI2_UITableViewDelete
- Leetcode_95 Unique Binary Search Trees II
- What's the difference between all the Selection Segues?
- iOS UITableView Cell动态高度(使用AutoLayout)
- UISwitch学习记录
- SUI
- iOS开发笔记--如何去掉UItableview header(footer)view黏性(sticky)
- StringBuilder与StringBuffer的区别
- Android开发UI之常用控件的使用
- Sublime Text Build System——编译运行Java
- iOS开发概述UIkit动力学
- Gradle DSL method not found: 'zipAlign()'/'runProguard()'/'jniDebugBuild()'/ 'renderscriptDebug()'