[iOS]把16进制颜色转换UIColor

- (void)viewDidLoad {

[super
viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

UIColor *color = [self
colorWithHexString:@"#81c52b"];

[self.view
setBackgroundColor:color];

}

- (UIColor *) colorWithHexString: (NSString *) stringToConvert

{

NSString *cString = [[stringToConvert
stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceAndNewlineCharacterSet]]
uppercaseString];//字符串处理

//例子,stringToConvert #ffffff

if ([cString
length] < 6)

return [UIColor
redColor];//如果非十六进制,返回白色

if ([cString
hasPrefix:@"#"])

cString = [cString substringFromIndex:1];//去掉头

if ([cString
length] != 6)//去头非十六进制,返回白色

return [UIColor
greenColor];

//分别取RGB的值

NSRange range;

range.location =
0;

range.length =
2;

NSString *rString = [cString
substringWithRange:range];

range.location =
2;

NSString *gString = [cString
substringWithRange:range];

range.location =
4;

NSString *bString = [cString
substringWithRange:range];

unsigned
int r, g, b;

//NSScanner把扫描出的制定的字符串转换成Int类型

[[NSScanner scannerWithString:rString] scanHexInt:&r];

[[NSScanner scannerWithString:gString] scanHexInt:&g];

[[NSScanner scannerWithString:bString] scanHexInt:&b];

//转换为UIColor

return [UIColor colorWithRed:((float) r /
255.0f)

green:((float) g /
255.0f)

blue:((float) b /
255.0f)

alpha:1.0f];

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-06 15:18:28

[iOS]把16进制颜色转换UIColor的相关文章

iOS开发-16进制颜色转换

项目中经常会用到颜色转换,有的是通过十六进制转成数字转颜色,想简单的点直接通过字符串转一下,简单扩展了一下分类UIColor,代码如下: +(UIColor *)colorWithHex:(NSString *)hexColor{ return [self colorWithHex:hexColor alpha:1.0f]; } //http://www.cnblogs.com/xiaofeixiang iOS技术交流:228407086 +(UIColor *)colorWithHex:(NS

iOS 将16进制字符串转换成UIColor

#pragma mark - 将16进制字符串转换成 UIColor - (UIColor *)generateColorObjectWithHex:(NSString *)hexString { unsigned int red = 0, green = 0, blue = 0; NSRange range = {0, 2}; range.location = 0; NSString *redString = [hexString substringWithRange:range]; NSSc

16进制颜色与UIColor互转

代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIView *view=[[UIView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)]; view.backgroundColor=[self colorWithHexString:@"e26562&q

iOS工具种之16进制颜色转为UIColor

 #define DEFAULT_VOID_COLOR [UIColor whiteColor] + (UIColor *)colorWithHexString:(NSString *)stringToConvert { NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];

日常练习之16进制颜色转换

1 function getRgb(x){ 2 var i,arr=[] 3 var getString=x.substring(1) 4 for(i=0;i<3;i++){ 5 arr[i]=parseInt(getString.substring(0,2),16) 6 getString=getString.substring(2) 7 } 8 return "rgb("+arr[0]+","+arr[1]+","+arr[2]+&qu

iOS开发中16进制颜色(html颜色值)字符串转为UIColor

//16进制颜色(html颜色值)字符串转为UIColor +(UIColor *) hexStringToColor: (NSString *) stringToConvert { NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString]; // String should be

iOS开发-从16进制颜色中获取UIColor

目前iOS中设置UIColor只能使用其枚举值.RGB等方法,不能直接将常用的16进制颜色值直接转为UIColor对象,所以写了点代码,将16进制颜色值转为UIColor. 代码如下, //头文件#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface TextServcie : NSObject +(UIColor *) getColorFromHEX:(NSString *)hex; @end .m

16进制颜色字符串转为UIColor

//16进制颜色(html颜色值)字符串转为UIColor +(UIColor *) hexStringToColor: (NSString *) stringToConvert { NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString]; // String should be

iOS 16进制字符串转换成int十进制

NSRange rangeErr; rangeErr.location = 6; rangeErr.length = 2; NSString *strings = [value substringWithRange:rangeErr]; int errCode = (int)strtoul([strings UTF8String],0,16);  //16进制字符串转换成int NSString *ascStr = [NSString stringWithFormat:@"%c",re