• 颜色转换:#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

  • 相关阅读:
    js笔记一:在HTML中使用javascript
    opencv排错总结
    《学习openCV》例程解析 ex_8_2 (轮廓)
    《学习openCV》例程解析 ex_9_3(codeBook模型实现背景减除)
    《学习openCV》例程解析 ex_9_1 (像素片段)
    《学习openCV》例程解析 ex_9_2(背景减除)
    《学习openCV》例程解析 ex_8_3 (轮廓)
    JavaScript鼠标事件
    sumlime text2 快捷键
    HTML字符实体(转)
  • 原文地址:https://www.cnblogs.com/niexiaobo/p/4646041.html
Copyright © 2020-2023  润新知