如何在拾取器背景中使用HEX值设置不同的颜色

时间:2016-06-11 12:59:32

标签: ios objective-c uipickerview uicolor

我想为拾取器视图背景设置三种不同的颜色 一个用于背景项目,一个用于当前在选择器中的项目(选定项目)。我将如何做到。

我有一个颜色的十六进制值(#01445)所以我必须将它设置为选择器背景。如何设置这些十六进制值。 而且我还必须将此颜色(#014455)值设置为选择器中的选定项目。

myPickerView = [[UIPickerView alloc]init];
myPickerView.dataSource = self;
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
myPickerView.tag=1;
[myPickerView setBackgroundColor:[self colorFromHexString:@"#014455"]];   //set different color here with hex value

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
                               initWithTitle:@"Done" style:UIBarButtonItemStyleDone
                               target:self action:@selector(done:)];


UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:
                      CGRectMake(0, self.view.frame.size.height-
                                 myPickerView.frame.size.height-(self.view.frame.size.width/6.4),self.view.frame.size.width,50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];

[toolBar setBackgroundColor:[self colorFromHexString:@"#ff7a03"]];    // i have hex value #ff7a03 so i have to set here

NSArray *toolbarItems = [NSArray arrayWithObjects:
                         doneButton, nil];
[toolBar setItems:toolbarItems];

这是我为在背景中使用她的颜色而创建的功能

-(UIColor *)colorFromHexString:(NSString *)hexString {
unsigned rgbValue = 0;
NSScanner *scanner = [NSScanner scannerWithString:hexString];
[scanner setScanLocation:1]; // bypass '#' character
[scanner scanHexInt:&rgbValue];
return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
  }

1 个答案:

答案 0 :(得分:0)

我认为你需要一个UIColorfromHex方法,这就是我想与你分享的内容

这是.h

#import <UIKit/UIKit.h>

@interface UIColor (Hex)

+(UIColor*)colorFromHex:(NSString*)hex;

@end

这是.m

#import "UIColor+Hex.h"

@implementation UIColor (Hex)

+(UIColor*)colorFromHex:(NSString*)hex
{
    NSString * cString = [hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].uppercaseString;

    if([cString hasPrefix:@"#"])
    {
        cString = [cString  substringFromIndex:1];
    }

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

    NSString * rString = [cString substringToIndex:2];
    NSString * gString = [cString substringWithRange:NSMakeRange(2, 2)];
    NSString * bString = [cString substringWithRange:NSMakeRange(4, 2)];

    unsigned int r,g,b = 0;

    [[[NSScanner alloc]initWithString:rString] scanHexInt:&r];
    [[[NSScanner alloc]initWithString:gString] scanHexInt:&g];
    [[[NSScanner alloc]initWithString:bString] scanHexInt:&b];

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


@end

然后你只需要导入这个UIColor + Hex.h并使用这个方法修改你的代码

像这样

myPickerView = [[UIPickerView alloc]init];
myPickerView.dataSource = self;
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
myPickerView.tag=1;
myPickerView.backgroundColor =[UIColor colorFromHex:@"#014455"];   //set different color here with hex value

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
                               initWithTitle:@"Done" style:UIBarButtonItemStyleDone
                               target:self action:@selector(done:)];


UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:
                      CGRectMake(0, self.view.frame.size.height-
                                 myPickerView.frame.size.height-(self.view.frame.size.width/6.4),self.view.frame.size.width,50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];

[toolBar setBackgroundColor:[UIColor colorFromHex:@"#ff7a03"]];  // i have hex value #ff7a03 so i have to set here

NSArray *toolbarItems = [NSArray arrayWithObjects:
                         doneButton, nil];
[toolBar setItems:toolbarItems];

我希望这有助于你