ios 如何为背景设置十六进制颜色代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6207329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:11:23  来源:igfitidea点击:

how to set hex color code for background

ioshexuicolor

提问by SuperString

Possible Duplicate:
How can I create a UIColor from a hex string?

可能的重复:
如何从十六进制字符串创建 UIColor?

I want to programmatically set the color of the UIView Background.

我想以编程方式设置 UIView 背景的颜色。

It doesn't seem like I can do it through Interfacebuilder. How should I do it if I want to set it to some hex code color?

似乎我不能通过 Interfacebuilder 做到这一点。如果我想将它设置为某种十六进制代码颜色,我该怎么做?

回答by WrightsCS

I like to use this little piece of code to use HTML web colors in my apps.

我喜欢使用这段代码在我的应用程序中使用 HTML 网页颜色。

Usage:

用法:

[self.view setBackgroundColor: [self colorWithHexString:@"FFFFFF"]]; /* white */

The Code:

编码:

-(UIColor*)colorWithHexString:(NSString*)hex  
{  
    NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];  

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

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

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

    // 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:1.0f];  
} 

回答by Tomasz Rybakiewicz

Here's simple category on UIColor that helps you create color from 8bit int value
and from hex value ("#a2ffc0").

这是 UIColor 上的简单类别,可帮助您从 8 位
整数值和十六进制值(“#a2ffc0”)创建颜色。

UIColor+CreateMethods.h

UIColor+CreateMethods.h

//
//  UIColor+CreateMethods.h
//
//  Created by Tomasz Rybakiewicz on 1/13/12.
//

#import <UIKit/UIKit.h>

@interface UIColor (CreateMethods)

// wrapper for [UIColor colorWithRed:green:blue:alpha:]
// values must be in range 0 - 255
+ (UIColor*)colorWith8BitRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha;

// Creates color using hex representation
// hex - must be in format: #FF00CC 
// alpha - must be in range 0.0 - 1.0
+ (UIColor*)colorWithHex:(NSString*)hex alpha:(CGFloat)alpha;

@end

UIColor+CreateMethods.m

UIColor+CreateMethods.m

//
//  UIColor+CreateMethods.m
//
//  Created by Tomasz Rybakiewicz on 1/13/12.
//

#import "UIColor+CreateMethods.h"

@implementation UIColor (CreateMethods)

+ (UIColor*)colorWith8BitRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha {
    return [UIColor colorWithRed:(red/255.0) green:(green/255.0) blue:(blue/255.0) alpha:alpha];
}

+ (UIColor*)colorWithHex:(NSString*)hex alpha:(CGFloat)alpha {

    assert(7 == [hex length]);
    assert('#' == [hex characterAtIndex:0]);

    NSString *redHex = [NSString stringWithFormat:@"0x%@", [hex substringWithRange:NSMakeRange(1, 2)]];
    NSString *greenHex = [NSString stringWithFormat:@"0x%@", [hex substringWithRange:NSMakeRange(3, 2)]];
    NSString *blueHex = [NSString stringWithFormat:@"0x%@", [hex substringWithRange:NSMakeRange(5, 2)]];

    unsigned redInt = 0;
    NSScanner *rScanner = [NSScanner scannerWithString:redHex];
    [rScanner scanHexInt:&redInt];

    unsigned greenInt = 0;
    NSScanner *gScanner = [NSScanner scannerWithString:greenHex];
    [gScanner scanHexInt:&greenInt];

    unsigned blueInt = 0;
    NSScanner *bScanner = [NSScanner scannerWithString:blueHex];
    [bScanner scanHexInt:&blueInt];

    return [UIColor colorWith8BitRed:redInt green:greenInt blue:blueInt alpha:alpha];
}

@end

Enjoy.

享受。

回答by Dan F

UIView's setBackgroundColormethod. You can create a UIColor from hex with UIColor initWithRed:green:blue:alphait takes floats so you'll need to do bitshifts and math to get the appropriate components. This is how I manage it

UIView 的setBackgroundColor方法。您可以使用 UIColor 从十六进制创建 UIColorinitWithRed:green:blue:alpha它需要浮点数,因此您需要进行位移和数学运算才能获得适当的组件。这就是我管理它的方式