iOS开发-16进制颜色转换

项目中经常会用到颜色转换,有的是通过十六进制转成数字转颜色,想简单的点直接通过字符串转一下,简单扩展了一下分类UIColor,代码如下:

+(UIColor *)colorWithHex:(NSString *)hexColor{
    return [self colorWithHex:hexColor alpha:1.0f];
}
//http://www.cnblogs.com/xiaofeixiang  iOS技术交流:228407086
+(UIColor *)colorWithHex:(NSString *)hexColor alpha:(float)alpha{
    //删除空格
    NSString *colorStr = [[hexColor stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([colorStr length] < 6||[colorStr length]>7)
    {
        return [UIColor clearColor];
    }
    //
    if ([colorStr hasPrefix:@"#"])
    {
        colorStr = [colorStr substringFromIndex:1];
    }

    NSRange range;
    range.location = 0;
    range.length = 2;
    //red
    NSString *redString = [colorStr substringWithRange:range];
    //green
    range.location = 2;
    NSString *greenString = [colorStr substringWithRange:range];
    //blue
    range.location = 4;
    NSString *blueString= [colorStr substringWithRange:range];

    // Scan values
    unsigned int red, green, blue;
    [[NSScanner scannerWithString:redString] scanHexInt:&red];
    [[NSScanner scannerWithString:greenString] scanHexInt:&green];
    [[NSScanner scannerWithString:blueString] scanHexInt:&blue];
    return [UIColor colorWithRed:((float)red/ 255.0f) green:((float)green/ 255.0f) blue:((float)blue/ 255.0f) alpha:alpha];
}

  

时间: 2024-11-10 00:02:39

iOS开发-16进制颜色转换的相关文章

[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:

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进制颜色转换

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进制字符串转换成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

C# RGB与16进制颜色转换方法

http://www.cnblogs.com/goldarch/archive/2010/08/16/1801053.html #region [颜色:16进制转成RGB]        /// <summary>        /// [颜色:16进制转成RGB]        /// </summary>        /// <param name="strColor">设置16进制颜色 [返回RGB]</param>       

前端笔试题:拍扁多维数组、提取URL中的参数变成对象、实现string的trim()函数、判断Email地址、16进制颜色转rgb格式

(1)拍扁数组 这是一个著名互联网公司今天的前端笔试题:多维数组维数不定,如[2,4,1,[2,3,4,[1,2],5,3],3,8]拍扁为[2,4,1,2,3,4,1,2,5,3,3,8].维数不定,应该要用递归: 答案: function flatten(arr){ var arrLength=arr.length; for(var i=0;i<arrLength;i++){ if(arr[i].constructor==Array){ arr.splice(i,1,flatten(arr[

随机16进制颜色生成方法

function colorOb() { var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];var sum =  "";for (var i = 0; i < 6; i++) {//向上取整,得到进制数var num= Math.ceil(Math.random() * 16);sum += arr[id];var sym =  "#&qu

将16进制字符串转换成16进制数据

var tb1 = Tb1.Text; if (string.IsNullOrEmpty(tb1)) { tb1 = "0000"; } var s1 = tb1.Substring(0, 2); var int_1 = Convert.ToInt32(s1, 16); var s2 = tb1.Substring(2, 2); var int_2 = Convert.ToInt32(s2, 16); var bt1 = Convert.ToByte(int_1); var bt2 =