2018-10-14 22:59:13 Zllvincent 阅读数 8338

iOS开发一般都是用到RGB颜色,美工给我们作图的时候往往给我们的色值都是十六进制的颜色如 #ff0000,如果先把16进制转换为RGB 的10 进制那将是非常麻烦的事,我们可以为 UIColor 添加一个扩展类(UIColor+Ext.swift)实现16进制颜色

import UIKit

extension UIColor {
  
    static func hexColor(_ hexValue:Int) -> UIColor {

    	return UIColor(red: ((CGFloat)((hexValue & 0xFF0000) >> 16)) / 255.0,
                   green: ((CGFloat)((hexValue & 0xFF00) >> 8)) / 255.0,
                   blue: ((CGFloat)(hexValue & 0xFF)) / 255.0,
                   alpha: 1.0)
    }
}

UIColor.hexColor(0xf55a5d)

UIColor.hexColor(0xf55a5d)

即可方便使用颜色

如果对您有帮助,点个赞支持 奶茶37.2℃ 创作更优秀的博文

2015-08-26 13:21:00 s133315202 阅读数 1582

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    


    UIColor *color = [selfcolorWithHexString:@"#81c52b"];

    [self.viewsetBackgroundColor:color];

}


- (UIColor *) colorWithHexString: (NSString *) stringToConvert

{

    NSString *cString = [[stringToConvertstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]]uppercaseString];//字符串处理

    //例子,stringToConvert #ffffff

    if ([cStringlength] <6)

        return [UIColorredColor];//如果非十六进制,返回白色

    if ([cStringhasPrefix:@"#"])

        cString = [cString substringFromIndex:1];//去掉头

    if ([cStringlength] !=6)//去头非十六进制,返回白色

        return [UIColorgreenColor];

    //分别取RGB的值

    NSRange range;

    range.location =0;

    range.length =2;

    NSString *rString = [cStringsubstringWithRange:range];

    

    range.location =2;

    NSString *gString = [cStringsubstringWithRange:range];

    

    range.location =4;

    NSString *bString = [cStringsubstringWithRange:range];

    

    unsignedint r, g, b;

    //NSScanner把扫描出的制定的字符串转换成Int类型

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

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

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

    //转换为UIColor

    return [UIColor colorWithRed:((float) r /255.0f)

                           green:((float) g /255.0f)

                            blue:((float) b /255.0f)

                           alpha:1.0f];

}

2015-03-31 16:11:39 pilgrim1385 阅读数 343
#define HEX_COLOR(c) [UIColor colorWithRed:((c>>16)&0xFF)/255.0 green:((c>>8)&0xFF)/255.0 blue:((c)&0xFF)/255.0 alpha:1.0]
如果颜色是333333
HEX_COLOR(0x333333)就是对应的颜色。
2018-06-21 17:07:59 aaaaazq 阅读数 2585
  • 字符串十六进制颜色值转换为UIColor:
/**
 16进制颜色转换为UIColor

 @param hexColor 16进制字符串(可以以0x开头,可以以#开头,也可以就是6位的16进制)
 @param opacity 透明度
 @return 16进制字符串对应的颜色
 */
+(UIColor *)colorWithHexString:(NSString *)hexColor alpha:(float)opacity{
    NSString * cString = [[hexColor stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];

    // String should be 6 or 8 characters
    if ([cString length] < 6) return [UIColor blackColor];

    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];

    if ([cString length] != 6) return [UIColor blackColor];

    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString * rString = [cString substringWithRange:range];

    range.location = 2;
    NSString * gString = [cString substringWithRange:range];

    range.location = 4;
    NSString * bString = [cString substringWithRange:range];

    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];

    return [UIColor colorWithRed:((float)r / 255.0f)
                           green:((float)g / 255.0f)
                            blue:((float)b / 255.0f)
                           alpha:opacity];
}
  • 十六进制数值转换为UIColor:
+(UIColor*)colorWithRGB:(NSUInteger)hex
                  alpha:(CGFloat)alpha
{
    float r, g, b, a;
    a = alpha;
    b = hex & 0x0000FF;
    hex = hex >> 8;
    g = hex & 0x0000FF;
    hex = hex >> 8;
    r = hex;

    return [UIColor colorWithRed:r/255.0f 
                           green:g/255.0f 
                           blue:b/255.0f 
                           alpha:a];
}

这两个方法建议以类方法写在UIColor的分类中,调用示例:

//字符串十六进制颜色值转换为UIColor
self.view.backgroundColor = [UIColor colorWithHexString:@"000000" alpha:0.4];
//十六进制数值转换为UIColor
self.view.backgroundColor = [UIColor colorWithRGB:0x242424 alpha:0.7];
2014-08-29 14:55:12 yexiekai 阅读数 361

可以把方法直接定义成宏在整个项目中使用:

#define ColorWithHexRGBA(rgbValue,a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16)/255.0) green:((float)((rgbValue & 0xFF00) >> 8)/255.0 blue:((float)((rgbValue & 0xFF))/255.0) alpha:(a)]


 

16进制颜色转换

阅读数 266

没有更多推荐了,返回首页