zoukankan      html  css  js  c++  java
  • 颜色转换:#hhhfff->UIColor (MHHexColoring)

     MHHexColoring为开发者快速获取想要的十六进制颜色(Hex Color) 

    查找16进制色码的网站: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

  • 相关阅读:
    C#图片存到资源文件,以及调用图片
    error C2039: “create”: 不是“cocos2d::GLView”的成员,出错解决办法
    error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1600”不匹配值“1800”
    vs2013激活码
    AppDelegate.h文件
    AppDelegate.cpp文件详解
    【最终版】Cocos2d-x&JS v3.7 发布!
    cocos2d-x 3.0 示例代码分析3:BaseTest
    vue li click
    me
  • 原文地址:https://www.cnblogs.com/niexiaobo/p/4646041.html
Copyright © 2011-2022 走看看