如何为 Section header 设置 RGB 颜色值 - IOS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11825863/
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
How to set RGB color value for Section header - IOS
提问by Feroz
I have tried with following code for my header view. Its not worked. How to set color other than default color values.
我为我的标题视图尝试了以下代码。它没有工作。如何设置默认颜色值以外的颜色。
[header_view setBackgroundColor:[UIColor colorWithRed:10. green:0 blue:0 alpha:0]];
回答by AJS
[header_view setBackgroundColor:[UIColor colorWithRed:10/255.0 green:0/255.0 blue:0/255.0 alpha:1]];
回答by bitmapdata.com
see the UIColor.h header. will be shown below.
请参阅 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
as the above color is same following code.
因为上面的颜色与下面的代码相同。
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];
.
.
.
if you want how to color rgb value apply to UIColor. see the below post
如果你想如何着色 rgb 值适用于 UIColor。看下面的帖子
you want color found wikipedia or else site.
你想要颜色找到维基百科或其他网站。
r, g, b values ??are apply as follows.
r、g、b 值适用如下。
[UIColor colorWithRed:158/255.0f green:253/255.0f blue:56/255.0f alpha:1.0f];
回答by Popeye
For anyone wanting use something simpler to my code below I have recently write a new category for UIColor
which can be found hereall you need to do is take the files called UIColor+extensions.h
and UIColor+extensions.m
and add them to your own project. This new category doesn't match the code below exactly as it has some other methods in it and I found a more efficient way of doing the colorWithHex:
method.
对于任何想使用更简单的东西到我的代码下面我最近写一个新的类别UIColor
可以发现这里所有你需要做的就是采取所谓的文件UIColor+extensions.h
和UIColor+extensions.m
,并将它们添加到自己的项目。这个新类别与下面的代码不完全匹配,因为它包含一些其他方法,我找到了一种更有效的方法来执行该colorWithHex:
方法。
Original answer原答案
You could also make your own Hexadecimal color converter by extending the UIColor
methods like so.
您还可以通过扩展这样的UIColor
方法来制作自己的十六进制颜色转换器。
UIColor_hex.h
UIColor_hex.h
#import <UIKit/UIColor.h>
@interface UIColor(MBCategory)
+ (UIColor *)colorWithHexString:(NSString *)hexStr;
@end
UIColor_hex.m
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
Then all you would need to do to is
那么你需要做的就是
UIColor *myHexColor = [UIColor colorWithHexString:@"#FFFFFF"];
*EDIT*You can also miss out the '#' if you want and the colorWithHexString will add it, you can also add the alpha on the end by by seperating it with one of these characters that has been set in cSet. So you could do
*编辑*如果需要,您也可以忽略“#”,colorWithHexString 将添加它,您还可以通过将其与 cSet 中设置的这些字符之一分开来在末尾添加 alpha。所以你可以这样做
UIColor *myHexColorWithAlpha = [UIColor colorWithHexString:@"#FFFFFF 0.4"];
UIColor *myHexColorWithAlpha = [UIColor colorWithHexString:@"#FFFFFF/0.4"];
UIColor *myHexColorWithAlpha = [UIColor colorWithHexString:@"#FFFFFF*0.4"];
etc
等等
Then to set the header you could do
然后设置标题你可以做
[header_view setBackgroundColor:[UIColor colorWithHexString:@"#FFFFFF"]];
回答by Hemant Dixit
try this code:
试试这个代码:
[header_view setBackgroundColor:[UIColor colorWithRed:149.0/255.0f green:149.0/255.0f
blue:149.0/255.0f alpha:1.0]];