http://www.color-hex.com

// 版权属于原作者 MHHexColoring

http://code4app.com/ios/MHHexColoring/548e9485933bf0a9738b6301

1、使用方法:

加入UIColor+HexString.h/m文件,导入头文件:
#import "UIColor+HexString.h"

获取颜色,返回UIColor:
[UIColor colorWithHexString:@"#ffffff"];

 

2、 UIColor+HexString.h

//  UIColor+HexString.h

//  shopbox

//

//  Created by Mohamed Hegab on 10/2/14.

//  Copyright (c) 2014 The Dark Dimension. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@interface UIColor (HexString)

 

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

 

@end

 

3、 UIColor+HexString.m

 

#import "UIColor+HexString.h"

 

 

@implementation UIColor (HexString)

 

+ (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;

}

 

+ (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:

            return nil;

    }

    return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];

}

 

@end

 

相关文章:

  • 2021-10-09
  • 2021-08-07
  • 2021-08-01
  • 2022-01-13
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-03-13
  • 2021-08-26
  • 2021-10-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案