2013-04-15 11:48:33 u010124617 阅读数 6395

在iOS开发中,经常有设计给到的颜色值是类似0xffaabb,或者#FFFFFF这种格式的,对于iOS内置的颜色处理UIColor来说,是无法直接处理使用的,因此需要做一个转换,转换方式如下:

+ (UIColor *)colorWithHexString:(NSString *)hexColorString {
    if ([hexColorString length] < 6) { //长度不合法
        return [UIColor blackColor];
    }
    NSString *tempString = [hexColorString lowercaseString];
    if ([tempString hasPrefix:@"0x"]) { //检查开头是0x
        tempString = [tempString substringFromIndex:2];
    } else if ([tempString hasPrefix:@"#"]) { //检查开头是#
        tempString = [tempString substringFromIndex:1];
    }
    if ([tempString length] != 6) {
        return [UIColor blackColor];
    }
    //分解三种颜色的值
    NSRange range = NSMakeRange(0, 2);
    NSString *rString = [tempString substringWithRange:range];
    range.location = 2;
    NSString *gString = [tempString substringWithRange:range];
    range.location = 4;
    NSString *bString = [tempString substringWithRange:range];
    //取三种颜色值
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float)r / 255.0f)
                           green:((float)g / 255.0f)
                            blue:((float)b / 255.0f)
                           alpha:1.0f];
}

另外,还有简化版的,用宏的方式:

#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define RGB(r,g,b) RGBA(r,g,b,1)

使用方式为:RGB(0xff,0xff,0xff)




2016-02-25 15:19:57 tuwanli125 阅读数 1132

//从十六进制字符串获取颜色,

//color:支持@“#123456” @“0X123456” @“123456”三种格式


+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha

{

    //删除字符串中的空格

    NSString *cString = [[colorstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]] uppercaseString];

    // String should be 6 or 8 characters

   if ([cString length] <6)

    {

       return [UIColorclearColor];

    }

    // strip 0X if it appears

    //如果是0x开头的,那么截取字符串,字符串从索引为2的位置开始,一直到末尾

   if ([cString hasPrefix:@"0X"])

    {

        cString = [cStringsubstringFromIndex:2];

    }

    //如果是#开头的,那么截取字符串,字符串从索引为1的位置开始,一直到末尾

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

    {

        cString = [cStringsubstringFromIndex:1];

    }

   if ([cString length] !=6)

    {

       return [UIColorclearColor];

    }

    

    // Separate into r, g, b substrings

   NSRange range;

    range.location =0;

    range.length =2;

   //r

   NSString *rString = [cString substringWithRange:range];

   //g

    range.location =2;

   NSString *gString = [cString substringWithRange:range];

   //b

    range.location =4;

   NSString *bString = [cString substringWithRange:range];

    

    // Scan values

   unsigned int r, g, b;

    [[NSScannerscannerWithString:rString] scanHexInt:&r];

    [[NSScannerscannerWithString:gString] scanHexInt:&g];

    [[NSScannerscannerWithString:bString] scanHexInt:&b];

   return [UIColorcolorWithRed:((float)r /255.0f) green:((float)g /255.0f) blue:((float)b /255.0f) alpha:alpha];

}


获取到颜色以后,画出这种颜色的图片

+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size

{

   CGRect rect = CGRectMake(0,0, size.width, size.height);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context =UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context,color.CGColor);

   CGContextFillRect(context, rect);

    UIImage *img =UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

   return img;

}


2013-07-02 17:54:14 piaol 阅读数 37
NSString *dictString = [dict JSONFragment];//组合成的。
用这个就行了。。。
dictString==={"content":"Sadgfdfg","phoneno":"","email":"1049055935@qq.com"}===
 
// 十六进制转换为普通字符串的。
+ (NSString *)stringFromHexString:(NSString *)hexString {  //
    
    char *myBuffer = (char *)malloc((int)[hexString length] / 2 + 1);
    bzero(myBuffer, [hexString length] / 2 + 1);
    for (int i = 0; i < [hexString length] - 1; i += 2) {
        unsigned int anInt;
        NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, 2)];
        NSScanner * scanner = [[[NSScanner alloc] initWithString:hexCharStr] autorelease];
        [scanner scanHexInt:&anInt];
        myBuffer[i / 2] = (char)anInt;
    }
    NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4];
    NSLog(@"------字符串=======%@",unicodeString);
    return unicodeString;
    
    
}

//普通字符串转换为十六进制的。

+ (NSString *)hexStringFromString:(NSString *)string{
    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
    Byte *bytes = (Byte *)[myD bytes];
    //下面是Byte 转换为16进制。
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++)
        
    {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        
        if([newHexStr length]==1)
            
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        
        else
            
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    }
    return hexStr;
}

 

 

 

 

2016-01-26 22:40:10 hero_wqb 阅读数 538

iOS 十六进制颜色字符串转为UIColor ,下面贴上代码:

#import <UIKit/UIKit.h>

@interface UIColor (PN)

/**
 *  16进制转化RGB
 */
+ (UIColor *)colorWithHexString:(NSString *)string;

/**
 *  随即色
 */
+ (UIColor *)randomColor;

@end


#import "UIColor+PN.h"

@implementation UIColor (PN)

+ (UIColor *)colorWithHexString:(NSString *)string
{
    if ([string hasPrefix:@"#"])
        string = [string substringFromIndex:1];
    
    // Separate into r, g, b substrings
    NSRange range;
    range.length = 2;
    
    range.location = 0;
    NSString *rString = [string substringWithRange:range];
    
    range.location = 2;
    NSString *gString = [string substringWithRange:range];
    
    range.location = 4;
    NSString *bString = [string substringWithRange:range];
    
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float)r/255.0f) green:((float)g/255.0f) blue:((float)b/255.0f) alpha:1];
}

+ (UIColor *)randomColor
{
    CGFloat red = arc4random_uniform(256);
    CGFloat green = arc4random_uniform(256);
    CGFloat blue = arc4random_uniform(256);
    
    return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1];
}

@end


2017-03-14 18:59:53 xuwenjiangshan 阅读数 192

对UIColor扩展,能通过从十六进制字符串获取颜色,支持@“#123456” @“0X123456” @“123456”三种格式

UIColor+Hex.h

#import <UIKit/UIKit.h>

#define RGBA_COLOR(R, G, B, A) [UIColor colorWithRed:((R) / 255.0f) green:((G) / 255.0f) blue:((B) / 255.0f) alpha:A]
#define RGB_COLOR(R, G, B) [UIColor colorWithRed:((R) / 255.0f) green:((G) / 255.0f) blue:((B) / 255.0f) alpha:1.0f]

@interface UIColor (Hex)

+ (UIColor *)colorWithHexString:(NSString *)color;

+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha;

@end

UIColor+Hex.m

#import "UIColor+Hex.h"

@implementation UIColor (Hex)

+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha
{
    //删除字符串中的空格
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6)
    {
        return [UIColor clearColor];
    }
    // strip 0X if it appears
    //如果是0x开头的,那么截取字符串,字符串从索引为2的位置开始,一直到末尾
    if ([cString hasPrefix:@"0X"])
    {
        cString = [cString substringFromIndex:2];
    }
    //如果是#开头的,那么截取字符串,字符串从索引为1的位置开始,一直到末尾
    if ([cString hasPrefix:@"#"])
    {
        cString = [cString substringFromIndex:1];
    }
    if ([cString length] != 6)
    {
        return [UIColor clearColor];
    }
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    //r
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];
}

//默认alpha值为1
+ (UIColor *)colorWithHexString:(NSString *)color
{
    return [self colorWithHexString:color alpha:1.0f];
}


@end


引用这个扩展,就可以直接用取色器的值进行赋值了。

//灰色
#define GRAYCOLOR1 [UIColor colorWithHexString:@"#eeeeee"]
#define GRAYCOLOR2 [UIColor colorWithHexString:@"#888888"]
//button颜色
#define COLORBUTTON [UIColor colorWithHexString:@"#d81e06"]




没有更多推荐了,返回首页