码迷,mamicode.com
首页 > 移动开发 > 详细

十六进制颜色转换为iOS可以用的UIColor

时间:2015-10-29 21:24:50      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

//

//  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

十六进制颜色转换为iOS可以用的UIColor

标签:

原文地址:http://www.cnblogs.com/dongdongyuan/p/4921631.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!