UI给图的时候给的是#f2f2f2 让我设置。没有你要的rgb。 所以只能自行解决封装了代码

HexColors.h

#import "TargetConditionals.h"

#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
  #import <UIKit/UIKit.h>
  #define HXColor UIColor
#else
  #import <Cocoa/Cocoa.h>
  #define HXColor NSColor
#endif

@interface HXColor (HexColorAddition)

+ (HXColor *)hx_colorWithHexString:(NSString *)hexString;
+ (HXColor *)hx_colorWithHexString:(NSString *)hexString alpha:(CGFloat)alpha;

+ (HXColor *)hx_colorWith8BitRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue;
+ (HXColor *)hx_colorWith8BitRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha;

@end

HexColors.m

#import "HexColors.h"

@implementation HXColor (HexColorAddition)

+ (HXColor *)hx_colorWithHexString:(NSString *)hexString
{
    // Check for hash and add the missing hash
    ])
    {
        hexString = [NSString stringWithFormat:@"#%@", hexString];
    }

    CGFloat alpha = 1.0;
     == hexString.length ||  == hexString.length) {
        NSString * alphaHex = [hexString substringWithRange:NSMakeRange(,  == hexString.length ?  : )];
         == alphaHex.length) alphaHex = [NSString stringWithFormat:@"%@%@", alphaHex, alphaHex];
        hexString = [NSString stringWithFormat: == hexString.length ?  : ]];
        unsigned alpha_u = [[self class] hx_hexValueToUnsigned:alphaHex];
        alpha = ((CGFloat) alpha_u) / 255.0;
    }

    return [[self class] hx_colorWithHexString:hexString alpha:alpha];
}

+ (HXColor *)hx_colorWithHexString:(NSString *)hexString alpha:(CGFloat)alpha
{
    ) {
        return nil;
    }

    // Check for hash and add the missing hash
    ])
    {
        hexString = [NSString stringWithFormat:@"#%@", hexString];
    }

    // check for string length
     != hexString.length &&  != hexString.length) {
        NSString *defaultHex    = [NSString stringWithFormat:@"0xff"];
        unsigned defaultInt = [[self class] hx_hexValueToUnsigned:defaultHex];

        HXColor *color = [HXColor hx_colorWith8BitRed:defaultInt green:defaultInt blue:defaultInt alpha:1.0];
        return color;
    }

    // check for 3 character HexStrings
    hexString = [[self class] hx_hexStringTransformFromThreeCharacters:hexString];

    NSString *redHex    = [NSString stringWithFormat:, )]];
    unsigned redInt = [[self class] hx_hexValueToUnsigned:redHex];

    NSString *greenHex  = [NSString stringWithFormat:, )]];
    unsigned greenInt = [[self class] hx_hexValueToUnsigned:greenHex];

    NSString *blueHex   = [NSString stringWithFormat:, )]];
    unsigned blueInt = [[self class] hx_hexValueToUnsigned:blueHex];

    HXColor *color = [HXColor hx_colorWith8BitRed:redInt green:greenInt blue:blueInt alpha:alpha];

    return color;
}

+ (HXColor *)hx_colorWith8BitRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue
{
    return [[self class] hx_colorWith8BitRed:red green:green blue:blue alpha:1.0];
}

+ (HXColor *)hx_colorWith8BitRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha
{
    HXColor *color = nil;
#if (TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE)
    color = [HXColor colorWithRed:( green:( blue:( alpha:alpha];
#else
    color = [HXColor colorWithCalibratedRed:( green:( blue:( alpha:alpha];
#endif

    return color;
}

+ (NSString *)hx_hexStringTransformFromThreeCharacters:(NSString *)hexString
{
    )
    {
        hexString = [NSString stringWithFormat:@"#%1$c%1$c%2$c%2$c%3$c%3$c",
                     [hexString characterAtIndex:],
                     [hexString characterAtIndex:],
                     [hexString characterAtIndex:]];

    }

    return hexString;
}

+ (unsigned)hx_hexValueToUnsigned:(NSString *)hexValue
{
    unsigned value = ;

    NSScanner *hexValueScanner = [NSScanner scannerWithString:hexValue];
    [hexValueScanner scanHexInt:&value];

    return value;
}

@end

调用

self.backgroundColor = [HXColor hx_colorWithHexString:@"ff5a60"];

iOS 设置#ffff 这种颜色的更多相关文章

  1. ios 设置状态栏文本颜色为白色

    1,在.plist文件中添加一个键值对:设置View controller-based status bar appearance的值为NO 2,在方法中 - (BOOL)application:(U ...

  2. iOS 设置状态栏的颜色

    1.在plist文件中设置如下属性: 2.在delegate中设置 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarSt ...

  3. iOS 设置不同的字体颜色

    //设置不同字体颜色 -(void)fuwenbenLabel:(UILabel *)labell FontNumber:(UIFont *)font AndRange:(NSRange)range ...

  4. iOS之 状态栏字体颜色的设置

    前一段时间接手一个项目后,熟悉的过程中发现了不少问题,其中有一个就是关于状态栏的问题. 我们都知道:状态栏字体颜色在不同界面不一样的,原因是系统设置的时候把状态栏的字体颜色的界面控制器设置的yes. ...

  5. iOS下使状态栏颜色与H5中背景色一致

    iOS 中有的页面也能会内嵌WebView,然后WebView中用H5做了一个导航,而iOS 中状态栏的颜色很难调整的与H5中导航颜色一致.如下图所示: 其实出现这种原因,主要是因为使用16进制颜色, ...

  6. iOS 设置View阴影

    iOS 设置View投影 需要设置 颜色 阴影半径 等元素 UIView *shadowView = [[UIView alloc] init]; shadowView.frame = CGRectM ...

  7. 设置statusBar状态栏颜色

    设置statusBar的[前景部分] 简单来说,就是设置显示电池电量.时间.网络部分标示的颜色, 这里只能设置两种颜色: 默认的黑色(UIStatusBarStyleDefault) 白色(UISta ...

  8. 设置placeholder字体颜色

    /*设置placeholder字体颜色*/::-webkit-input-placeholder{ color: #FFF;}:-ms-input-placeholder{ color: #FFF;} ...

  9. IOS中十六进制的颜色转换为UIColor

    IOS中十六进制的颜色转换为UIColor #pragma mark - 颜色转换 IOS中十六进制的颜色转换为UIColor + (UIColor *) colorWithHexString: (N ...

随机推荐

  1. 使用Java操作文本文件的方法详解

    使用Java操作文本文件的方法详解 摘要: 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而 ...

  2. chart.js在html中画曲线图

    http://www.bootcss.com/p/chart.js/docs/ http://www.chartjs.org/docs/   中有详细讲解 一.简介 Chart.js是一个基于HTML ...

  3. python遍历字典元素

    a={'a':{'b':{'c':{'d':'e'}},'f':'g'},'h':'i'} def show(myMap): for str in myMap.keys(): secondDict=m ...

  4. CSS排序工具csscomb

    一. css写久了以后感觉需要按照某种规则或者说顺序来,便于开发和维护.O(∩_∩)O哈哈~ 找到一个 http://csscomb.com/ CSS排序总结: 1. 对css样式排序; 2. 按样式 ...

  5. 单位冲击响应与频响以及FIR实现代码(C语言)(转)

    源:FIR数字滤波器C语言 1.单位冲击响应与频响 就如同之前所说的一样,使用下图所示的单位冲击响应,所设计的滤波器,是无法实现的. 现在,让我们看看其这个滤波器的频响.所谓频响,就是计算其单位冲击响 ...

  6. iOS强制切换横屏、竖屏

    切换横竖屏最直接的方式是调用device的setOrientation方法.但是从sdk3.0以后,这个方法转为似有API,如果要上AppStore的话,要慎用! if ([[UIDevice cur ...

  7. thinkphp所有参数配置

    /* 项目设定 */ 'APP_DEBUG' => false, // 是否开启调试模式 'APP_DOMAIN_DEPLOY' => false, // 是否使用独立域名部署项目 'AP ...

  8. 【转】10个重要的Linux ps命令实战

    Linux作为Unix的衍生操作系统,Linux内建有查看当前进程的工具ps.这个工具能在命令行中使用. PS 命令是什么 查看它的man手册可以看到,ps命令能够给出当前系统中进程的快照.它能捕获系 ...

  9. 分页。php 引用代码

    <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 private $lis ...

  10. Servlet_ResponseHeader

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcepti ...