c_cpp Objective-C类将十六进制字符串转换为UIColor。支持#RGB#ARGB #RRGGBB #AARRGGBBUsage:[UIColor colorWithHexString:

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp Objective-C类将十六进制字符串转换为UIColor。支持#RGB#ARGB #RRGGBB #AARRGGBBUsage:[UIColor colorWithHexString:相关的知识,希望对你有一定的参考价值。

//
//  UIColor+HexString.m
//
//  Created by Zhu Yuzhou on 1/20/13.
//

#import "UIColor+HexString.h"

@interface UIColor()

+ (CGFloat) colorComponentFrom: (NSString *) string start: (NSUInteger) start length: (NSUInteger) length;

@end


@implementation UIColor(HexString)

+ (UIColor *) colorWithHexString: (NSString *) hexString
{
    NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];
    CGFloat alpha, red, blue, green;
    switch ([colorString length]) {
        case 3: // #RGB
            alpha = 1.0f;
            red   = [self colorComponentFrom: colorString start: 0 length: 1];
            green = [self colorComponentFrom: colorString start: 1 length: 1];
            blue  = [self colorComponentFrom: colorString start: 2 length: 1];
            break;
        case 4: // #ARGB
            alpha = [self colorComponentFrom: colorString start: 0 length: 1];
            red   = [self colorComponentFrom: colorString start: 1 length: 1];
            green = [self colorComponentFrom: colorString start: 2 length: 1];
            blue  = [self colorComponentFrom: colorString start: 3 length: 1];
            break;
        case 6: // #RRGGBB
            alpha = 1.0f;
            red   = [self colorComponentFrom: colorString start: 0 length: 2];
            green = [self colorComponentFrom: colorString start: 2 length: 2];
            blue  = [self colorComponentFrom: colorString start: 4 length: 2];
            break;
        case 8: // #AARRGGBB
            alpha = [self colorComponentFrom: colorString start: 0 length: 2];
            red   = [self colorComponentFrom: colorString start: 2 length: 2];
            green = [self colorComponentFrom: colorString start: 4 length: 2];
            blue  = [self colorComponentFrom: colorString start: 6 length: 2];
            break;
        default:
            [NSException raise:@"Invalid color value" format: @"Color value %@ is invalid.  It should be a hex value of the form #RBG, #ARGB, #RRGGBB, or #AARRGGBB", hexString];
            break;
    }
    return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
}

+ (CGFloat) colorComponentFrom: (NSString *) string start: (NSUInteger) start length: (NSUInteger) length
{
    NSString *substring = [string substringWithRange: NSMakeRange(start, length)];
    NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
    unsigned hexComponent;
    [[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
    return hexComponent / 255.0;
}

@end
//
//  UIColor+HexString.h
//  Class to convert hex string to UIColor
//  Support #RGB # ARGB #RRGGBB #AARRGGBB
//  Usage: [UIColor colorWithHexString:@"#f5e6a1"];
//  Created by Zhu Yuzhou on 1/20/13.
//

#import <UIKit/UIKit.h>

@interface UIColor (HexString)

+ (UIColor *) colorWithHexString: (NSString *) hexString;
+ (CGFloat) colorComponentFrom: (NSString *) string start: (NSUInteger) start length: (NSUInteger) length;

@end

以上是关于c_cpp Objective-C类将十六进制字符串转换为UIColor。支持#RGB#ARGB #RRGGBB #AARRGGBBUsage:[UIColor colorWithHexString:的主要内容,如果未能解决你的问题,请参考以下文章

Objective-C将十六进制字符串解析为整数

c_cpp 用于生成十六进制字符串表示的NSData类别

c_cpp 从给定模式生成所有二进制字符串

c_cpp [十进制数转罗马数字]将一个十进制数字符串转换为罗马数字字符串#字符串处理

c_cpp 来自十六进制的UIColor没有字符串解析,例如RGB(0x4C2BFF)

c_cpp [罗马数字转十进制数]接收罗马数字字符串,返回一个int型十进制数#字符串处理