美文网首页
获取16进制颜色 方法如(#FA8072)等

获取16进制颜色 方法如(#FA8072)等

作者: JaneEyre3X | 来源:发表于2017-11-01 09:52 被阅读0次

    首先创建一个类 继承与UIview 名字先叫做 UIColor+ColorChange 吧!

    我们在UIColor+ColorChange.h 文件中写

    #ifndef UIColor_ColorChange_h
    #define UIColor_ColorChange_h
    #import <UIKit/UIKit.h>
    
    @interface UIColor (ColorChange)
    
    // 颜色转换:iOS中(以#开头)十六进制的颜色转换为UIColor(RGB)
    + (UIColor *) colorWithHexString: (NSString *)color;
    
    @end
    
    #endif /* UIColor_ColorChange_h */
    

    之后我们在UIColor+ColorChange.m中写

    #import <Foundation/Foundation.h>
    #import "UIColor+ColorChange.h"
    
    @implementation UIColor (ColorChange)
    
    + (UIColor *) colorWithHexString: (NSString *)color
    {
        NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
        
        // String should be 6 or 8 characters
        if ([cString length] < 6) {
            return [UIColor clearColor];
        }
        // 判断前缀
        if ([cString hasPrefix:@"0X"])
            cString = [cString substringFromIndex:2];
        if ([cString hasPrefix:@"#"])
            cString = [cString substringFromIndex:1];
        if ([cString length] != 6)
            return [UIColor clearColor];
        // 从六位数值中找到RGB对应的位数并转换
        NSRange range;
        range.location = 0;
        range.length = 2;
        //R、G、B
        NSString *rString = [cString substringWithRange:range];
        range.location = 2;
        NSString *gString = [cString substringWithRange:range];
        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:1.0f];
    }
    @end
    

    之后在你所界面中 (引入头文件)! 定义宏定义 !!! 定义宏定义 !!! 定义宏定义 !!!
    ![Uploading 69CE7C9B-69CF-4E0E-AB50-54D5710B1A2F_588784.png . . .]

    #define HEXCOLOR(hex) [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16)) / 255.0 green:((float)((hex & 0xFF00) >> 8)) / 255.0 blue:((float)(hex & 0xFF)) / 255.0 alpha:1]
    

    整体代码如下

    #import "ViewController.h"
    #import "UIColor+ColorChange.h"
    #define HEXCOLOR(hex) [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16)) / 255.0 green:((float)((hex & 0xFF00) >> 8)) / 255.0 blue:((float)(hex & 0xFF)) / 255.0 alpha:1]
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor colorWithHexString:@"#00F5FF"];
        
        UILabel * colorlabel = [[UILabel alloc]initWithFrame:CGRectMake(0,60, self.view.frame.size.width, 40)];
        colorlabel.backgroundColor = [UIColor colorWithHexString:@"#FFFACD"];
        [self.view addSubview:colorlabel];
        
        UILabel * colorlabel1 = [[UILabel alloc]initWithFrame:CGRectMake(0,180, self.view.frame.size.width, 40)];
        colorlabel1.backgroundColor = [UIColor colorWithHexString:@"#FA8072"];
        [self.view addSubview:colorlabel1];
    }
    @end
    
    

    颜色色值表
    file:///Users/apple/Downloads/RGB颜色对照表.html

    22.png

    感觉有帮助的话给小编点个喜欢哈

           🌹人生应用七彩涂抹,生活应用踏实铺陈,日子应用充实贯穿。心中怀有使命感,失落感,危机感的人才是不平凡的人🌹
    

    共勉

    相关文章

      网友评论

          本文标题:获取16进制颜色 方法如(#FA8072)等

          本文链接:https://www.haomeiwen.com/subject/bzjwpxtx.html