iOS 设置#ffff 这种颜色

时间:2023-03-09 08:42:56
iOS 设置#ffff 这种颜色

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"];