2012-08-06 106 views
4

我已經嘗試使用以下代碼爲我的標題視圖。它沒有工作。如何設置不同於默認顏色值的顏色。如何設置部分標題的RGB顏色值 - IOS

[header_view setBackgroundColor:[UIColor colorWithRed:10. green:0 blue:0 alpha:0]]; 

回答

10
[header_view setBackgroundColor:[UIColor colorWithRed:10/255.0 green:0/255.0 blue:0/255.0 alpha:1]]; 
1

試試這個代碼:

[header_view setBackgroundColor:[UIColor colorWithRed:149.0/255.0f green:149.0/255.0f 
blue:149.0/255.0f alpha:1.0]]; 
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。看到下面的帖子

你想要顏色發現維基百科或其他網站。

enter image description here

R,G,B值是應用如下。

[UIColor colorWithRed:158/255.0f green:253/255.0f blue:56/255.0f alpha:1.0f]; 

enter image description here

4

任何人想要使用更簡單的東西到我的代碼下面我最近寫了UIColor一個新的類別可以發現here所有你需要做的就是採取所謂的UIColor+extensions.hUIColor+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中設置的這些字符之一來分開來添加字母。所以,你可以做

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"]];