我已尝试使用以下代码查看标题视图。它不起作用。如何设置默认颜色值以外的颜色。
[header_view setBackgroundColor:[UIColor colorWithRed:10. green:0 blue:0 alpha:0]];
答案 0 :(得分:11)
[header_view setBackgroundColor:[UIColor colorWithRed:10/255.0 green:0/255.0 blue:0/255.0 alpha:1]];
答案 1 :(得分:7)
请参阅UIColor.h标头。将在下面显示。
+ (UIColor *)blackColor; // 0.0 white
+ (UIColor *)darkGrayColor; // 0.333 white
+ (UIColor *)lightGrayColor; // 0.667 white
+ (UIColor *)whiteColor; // 1.0 white
+ (UIColor *)grayColor; // 0.5 white
+ (UIColor *)redColor; // 1.0, 0.0, 0.0 RGB
+ (UIColor *)greenColor; // 0.0, 1.0, 0.0 RGB
+ (UIColor *)blueColor; // 0.0, 0.0, 1.0 RGB
+ (UIColor *)cyanColor; // 0.0, 1.0, 1.0 RGB
+ (UIColor *)yellowColor; // 1.0, 1.0, 0.0 RGB
+ (UIColor *)magentaColor; // 1.0, 0.0, 1.0 RGB
+ (UIColor *)orangeColor; // 1.0, 0.5, 0.0 RGB
+ (UIColor *)purpleColor; // 0.5, 0.0, 0.5 RGB
+ (UIColor *)brownColor; // 0.6, 0.4, 0.2 RGB
+ (UIColor *)clearColor; // 0.0 white, 0.0 alpha
以下颜色与以下颜色相同。
black: [UIColor colorWithWhite:0.0f alpha:1.0f];
darkGray: [UIColor colorWithWhite:0.333f alpha:1.0f];
lightGray: [UIColor colorWithWhite:0.667f alpha:1.0f];
white: [UIColor colorWithWhite:1.0f alpha:1.0f];
gray: [UIColor colorWithWhite:0.5f alpha:1.0f];
red: [UIColor colorWithRed:255/255.0f green:0/255.0f blue:0/255.0f alpha:1.0f];
green: [UIColor colorWithRed:0/255.0f green:255/255.0f blue:0/255.0f alpha:1.0f];
blue: [UIColor colorWithRed:0/255.0f green:0/255.0f blue:255/255.0f alpha:1.0f];
.
.
.
如果你想如何将颜色rgb值应用于UIColor。见下面的帖子
你想要找到维基百科或网站的颜色。
r,g,b值适用如下。
[UIColor colorWithRed:158/255.0f green:253/255.0f blue:56/255.0f alpha:1.0f];
答案 2 :(得分:4)
对于想要在我的代码中使用更简单的内容的人,我最近为UIColor
编写了一个新类别,可以找到here您需要做的就是获取名为UIColor+extensions.h
的文件和UIColor+extensions.m
并将它们添加到您自己的项目中。这个新类别与下面的代码完全不同,因为它有一些其他方法,我找到了一种更有效的方法来执行colorWithHex:
方法。
<小时/> 原始答案
您也可以通过扩展UIColor
方法来制作自己的十六进制颜色转换器。
UIColor_hex.h
#import <UIKit/UIColor.h>
@interface UIColor(MBCategory)
+ (UIColor *)colorWithHexString:(NSString *)hexStr;
@end
UIColor_hex.m
#import "UIColor_Hex.h"
@interface UIColor(HexConverterCategory)
// Takes 0x123456
+ (UIColor *)colorWithHex:(UInt32)color andAlpha:(float)alpha;
@end
@implementation UIColor(HexConverterCategory)
+ (UIColor *)colorWithHex:(UInt32)color andAlpha:(float)alpha
{
unsigned char r, g, b;
b = color & 0xFF;
g = (color >> 8) & 0xFF;
r = (color >> 16) & 0xFF;
return [UIColor colorWithRed:(float)r/255.0f green:(float)g/255.0f blue:(float)b/255.0f alpha:alpha];
}
@end
@implementation UIColor(MBCategory)
+ (UIColor *)colorWithHexString:(NSString *)hexStr
{
float alpha;
NSString *newHexStr;
NSCharacterSet *cSet = [NSCharacterSet characterSetWithCharactersInString:@"/-_,~^*&\\ "];
if(![hexStr hasPrefix:@"#"]) hexStr = [NSString stringWithFormat:@"#%@", hexStr];
if([hexStr rangeOfCharacterFromSet:cSet].location != NSNotFound) {
NSScanner *scn = [NSScanner scannerWithString:hexStr];
[scn scanUpToCharactersFromSet:cSet intoString:&newHexStr];
alpha = [[[hexStr componentsSeparatedByCharactersInSet:cSet] lastObject] floatValue];
} else {
newHexStr = hexStr;
alpha = 1.0f;
}
const char *cStr = [newHexStr cStringUsingEncoding:NSASCIIStringEncoding];
long x = strtol(cStr+1, NULL, 16);
return [UIColor colorWithHex:x andAlpha:alpha];
}
@end
然后您需要做的就是
UIColor *myHexColor = [UIColor colorWithHexString:@"#FFFFFF"];
*的 修改 * 强> 如果需要,你也可以错过'#',而colorWithHexString会添加它,你也可以通过用cSet中设置的其中一个字符分隔它来添加alpha。所以你可以做到
UIColor *myHexColorWithAlpha = [UIColor colorWithHexString:@"#FFFFFF 0.4"];
UIColor *myHexColorWithAlpha = [UIColor colorWithHexString:@"#FFFFFF/0.4"];
UIColor *myHexColorWithAlpha = [UIColor colorWithHexString:@"#FFFFFF*0.4"];
等
然后设置你可以做的标题
[header_view setBackgroundColor:[UIColor colorWithHexString:@"#FFFFFF"]];
答案 3 :(得分:1)
试试这段代码:
[header_view setBackgroundColor:[UIColor colorWithRed:149.0/255.0f green:149.0/255.0f
blue:149.0/255.0f alpha:1.0]];