您的位置:首页 > 产品设计 > UI/UE

颜色转换:#hhhfff->UIColor (MHHexColoring)

2015-07-14 17:30 537 查看

MHHexColoring为开发者快速获取想要的十六进制颜色(Hex Color)

查找16进制色码的网站:http://www.color-hex.com

// 版权属于原作者 MHHexColoring

http://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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: