从16进制颜色中获取UIColor

来源:互联网 发布:厦门理工软件学院地址 编辑:程序博客网 时间:2024/06/05 17:35
<span style="font-size:18px;">/头文件#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@interface TextServcie : NSObject+(UIColor *) getColorFromHEX:(NSString *)hex;@end</span>

.m
<span style="font-size:18px;">@implementation TextServcie+(UIColor *)getcolorfromHEX:(NSString *)hex{    hex= [[hex uppercaseString] substringFromIndex:1];    CGFloat valueArray[3];    NSArray *strArray=[NSArray arrayWithObjects:[hex substringWithRange:NSMakeRange(0, 2)],[hex substringWithRange:NSMakeRange(2, 2)],[hex substringWithRange:NSMakeRange(4, 2)] ,nil];    for( int i=0;i<strArray.count;i++){        hex=strArray[i];        CGFloat value=([hex characterAtIndex:0]>'9'?[hex characterAtIndex:0]-'A'+10:[hex characterAtIndex:0]-'0')*16.0f+([hex characterAtIndex:1]>'9'?[hex characterAtIndex:1]-'A'+10:[hex characterAtIndex:1]-'0');        valueArray[i]=value;    }    return [UIColor colorWithRed:valueArray[0]/255 green:valueArray[1]/255 blue:valueArray[2]/255 alpha:1];}@end</span>


0 0