zoukankan      html  css  js  c++  java
  • 16进制字符串转换成UIColor,返回UIColor

    1.建立一个UIColor的category,命名为UIColor+Helper

    然后在UIColor+Helper.h中:

    //16进制字符串转换成UIColor,返回UIColor

    + (UIColor *)colorWithHexStr:(NSString *)colorStr;

    在UIColor+Helper.m中:

    //16进制字符串转换成UIColor

    + (UIColor *)colorWithHexStr:(NSString *)colorStr{

        NSString * redStr = [colorStr substringWithRange:NSMakeRange(0, 2)];

        NSScanner * redScanner = [NSScanner scannerWithString:redStr];

        unsigned int redIntValue;

        [redScanner scanHexInt:&redIntValue];

        

        NSString * greenStr = [colorStr substringWithRange:NSMakeRange(2, 2)];

        NSScanner * greenScanner = [NSScanner scannerWithString:greenStr];

        unsigned int greenIntValue;

        [greenScanner scanHexInt:&greenIntValue];

        

        NSString * blueStr = [colorStr substringWithRange:NSMakeRange(4, 2)];

        NSScanner * blueScanner = [NSScanner scannerWithString:blueStr];

        unsigned int blueIntValue;

        [blueScanner scanHexInt:&blueIntValue];

        

        return [UIColor colorWithRed:redIntValue/255.0 green:greenIntValue/255.0 blue:blueIntValue/255.0 alpha:1];

    }

     多数用于服务器后端传值,然后前端将16进制的色值展示出来

  • 相关阅读:
    C语言运算符优先级
    【科创网0184】选小寿星
    结构体的基本使用
    10月18日的疑问
    【从简单开始】简单的A+B问题
    【从简单开始】五层小山
    【从简单开始】Hello World
    基本递归(3)求阶乘
    基本递归(2)汉诺塔
    基本递归(1)勒让德多项式
  • 原文地址:https://www.cnblogs.com/zhouyantongiOSDev/p/4371454.html
Copyright © 2011-2022 走看看