UIColor的16进制色值,16进制字符串扩展


@interface UIColor (Hex)

/**

 *  16进制字符串转化为颜色

 *  @param hexStr 16进制字符串 支持@"0x..." @"0X..." @"#..." @"..."

 *  @return UIColor

 */

+ (UIColor *)colorWithHexString:(NSString *)hexStr;

/**

 *  16进制字符串转化为颜色

 *  @param hexStr 16进制字符串

 *  @param alpha  透明度

 *  @return UIColor

 */

+ (UIColor *)colorWithHexString:(NSString *)hexStr alpha:(NSInteger)alpha;

@end



/**

 *  16进制色值转化颜色

 *  @param hexInteger 16进制色值

 *  @return UIColor

 */

+ (UIColor *)colorWithHex:(NSInteger)hexInteger;

/**

 *  16进制色值转化颜色

 *  @param hexInteger 16进制色值

 *  @param alpha      透明度

 *  @return UIColor

 */

+ (UIColor *)colorWithHex:(NSInteger)hexInteger alpha:(NSInteger)alpha;


@implementation UIColor (Hex)


/**

 *  16进制字符串转化为颜色

 */

+ (UIColor *)colorWithHexString:(NSString *)hexStr {

    //字符串去除两端空格,变大写

    NSString *colorStr = [[hexStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];

    

    //至少6

    if (colorStr.length < 6) {

        return [UIColor clearColor];

    }

    //进一步判断,处理

    if ([colorStr hasPrefix:@"0X"]) {

        colorStr = [colorStr substringFromIndex:2];

    }

    if ([colorStr hasPrefix:@"#"]) {

        colorStr = [colorStr substringFromIndex:1];

    }

    if (colorStr.length < 6) {

        return [UIColor clearColor];

    }

    

    NSRange range;

    range.length = 2;

    //R

    range.location = 0;

    NSString *rString = [colorStr substringWithRange:range];

    //G

    range.location = 2;

    NSString *gString = [colorStr substringWithRange:range];

    //B

    range.location = 4;

    NSString *bString = [colorStr substringWithRange:range];

    

    unsigned int r, g, b;

    [[NSScanner scannerWithString:rString] scanHexInt:&r];

    [[NSScanner scannerWithString:bString] scanHexInt:&g];

    [[NSScanner scannerWithString:gString] scanHexInt:&b];

    

    return [UIColor colorWithRed:((float)r / 255.0) green:((float)g / 255.0) blue:((float)b / 255.0) alpha:1];

}


+ (UIColor *)colorWithHexString:(NSString *)hexStr alpha:(NSInteger)alpha {

    //字符串去除两端空格,变大写

    NSString *colorStr = [[hexStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];

    

    //至少6

    if (colorStr.length < 6) {

        return [UIColor clearColor];

    }

    //进一步判断,处理

    if ([colorStr hasPrefix:@"0X"]) {

        colorStr = [colorStr substringFromIndex:2];

    }

    if ([colorStr hasPrefix:@"#"]) {

        colorStr = [colorStr substringFromIndex:1];

    }

    if (colorStr.length < 6) {

        return [UIColor clearColor];

    }

    

    NSRange range;

    range.length = 2;

    //R

    range.location = 0;

    NSString *rString = [colorStr substringWithRange:range];

    //G

    range.location = 2;

    NSString *gString = [colorStr substringWithRange:range];

    //B

    range.location = 4;

    NSString *bString = [colorStr substringWithRange:range];

    

    unsigned int r, g, b;

    [[NSScanner scannerWithString:rString] scanHexInt:&r];

    [[NSScanner scannerWithString:bString] scanHexInt:&g];

    [[NSScanner scannerWithString:gString] scanHexInt:&b];

    

    return [UIColor colorWithRed:((float)r / 255.0) green:((float)g / 255.0) blue:((float)b / 255.0) alpha:alpha];

}



/**

 *  16进制色值转化颜色

 */

+ (UIColor *)colorWithHex:(NSInteger)hexInteger {

    return [UIColor colorWithRed:((float)((hexInteger & 0xFF0000) >> 16)) / 255.0

                           green:((float)((hexInteger & 0xFF00) >> 8)) / 255.0

                            blue:((float)(hexInteger & 0xFF)) alpha:1];

}


+ (UIColor *)colorWithHex:(NSInteger)hexInteger alpha:(NSInteger)alpha {

    return [UIColor colorWithRed:((float)((hexInteger & 0xFF0000) >> 16)) / 255.0

                           green:((float)((hexInteger & 0xFF00) >> 8)) / 255.0

                            blue:((float)(hexInteger & 0xFF)) alpha:alpha];

}


@end


(1)宏定义RGB_COLOR和RGBA_COLOR可以设置颜色

(2)UIColor+Hex扩展可以设置颜色

(3)导航栏上面的BarButtonItem怎么设置为Button

(4)Button一些常用和不常用的属性设置


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值