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. (中等) POJ 1054 The Troublesome Frog,记忆化搜索。

    Description In Korea, the naughtiness of the cheonggaeguri, a small frog, is legendary. This is a we ...

  2. (简单) HDU 2612 Find a way,BFS。

    Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Nin ...

  3. storybody中页面跳转

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if ([segue.destinationViewCont ...

  4. 源码解析-knockout源码准备

    准备包括心理和资源两方面. 心理 我看过一句话说,当你用一个框架时,不要忙着看一遍使用教程就开始写项目,先去看看框架原理. 这句话我深以为然.现今前端快速发展,很多前端攻城狮都很茫然:框架更新太快了, ...

  5. STM32-USB详细使用说明(转)

    源:STM32-USB详细使用说明 附件HID的双向通信 亮点STM32开发板充实了USBHID数据发送和接收例程(STM32固件库3.5 USB库3.4)

  6. 打包程序时的证书问题(上传APP就出现Missing iOS Distribution signing indetity for)

    现象: 解决办法: 1.删除本地钥匙串中的这个文件,注意“系统”中的同名文件也必须删除 2.进入http://www.apple.com/certificateauthority/ 下载新的(WWDR ...

  7. Android与JNI(二) ---- Java调用C++ 动态调用

    目录: 1. 简介 2. JNI 组件的入口函数 3. 使用 registerNativeMethods 方法 4. 测试 5. JNI 帮助方法 6. 参考资料 1. 简介 Android与JNI( ...

  8. onethink的插件扩展

    2014年7月25日 07:22:35 编写一个文章可以同步到微博的插件 思路:首先要进行微博的绑定,获得令牌.然后调用微博的api即可. 2014年7月25日 08:40:27 服务器与本地有区别. ...

  9. sql语句的截取字符串下标是从1开始

    这里的指的sqliter数据库 substr函数 substr(date,1,10)

  10. System.Web.UI.WebControls的Web服务器控件

    calendar[英][ˈkælɪndə(r)][美][ˈkæləndɚ]n.日历; 历法; 日程表; (一年之中的)重大事件(或重要日期)一览表; vt.把…记入日程表中; 把…列入表中; 为(文件 ...