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

时间:2011-06-01 20:36:47

标签: ios hex uicolor

  

可能重复:
  How can I create a UIColor from a hex string?

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

似乎我不能通过Interfacebuilder来实现。如果我想将它设置为某种十六进制代码颜色,我该怎么办呢?

3 个答案:

答案 0 :(得分:198)

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

用法:

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

守则:

-(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];  
} 

答案 1 :(得分:31)

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

<强>的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
//
//  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

享受。

答案 2 :(得分:3)

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