2012-01-11 25 views
6

我有幾個使用Inteface Builder創建的黃色按鈕。所有顏色都一樣。目前我在每個xib中聲明顏色。我可以在全球範圍內宣佈它並在所有xib中重複使用嗎?我可以在Interface Builder中重用顏色嗎?

+1

「界面構建器」中的每個人都回答了這個問題的每個人都無法理解?奇怪的。 – 2013-08-11 14:11:22

+1

@MarkAmery這意味着,唯一的方法是使用_code_。 – Tricertops 2013-08-11 16:25:30

+0

真的很無聊,沒有辦法做到這一點。誰想要在IB中逐個調整100頁的顏色? – Jonny 2015-12-07 14:15:58

回答

-1

絕對是!

創建一個單獨的對象(你可以叫它OksanaColor,要冷靜)...

...或者,如果你真懶,只讀屬性UIColor,您可以從您的訪問應用程序委託。

-1

您也可以在UIColor上添加一個類別,這樣您就可以像使用UIColor一樣使用它。例如,在我的應用程序中,我添加了一個名爲ApplicationColors的新文件,其中包含我所有的應用程序顏色。

@interface UIColor (ApplicationColours) 

+(UIColor *)savaColor; 

@end 

實現:

@implementation UIColor (ApplicationColours) 

+(UIColor *)savaColor 
{ 
    return [UIColor colorWithRed:228.0f/255.0f green:86.0f/255.0f blue:86.0f/255.0f alpha:1.0f]; 

} 

@end 

然後用它在我的應用程序,我導入ApplicationColours.h並使用相同的任何其他的UIColor。即:

label.textColor = [UIColor savaColor]; 
+1

您可能希望使+ savaColor爲該顏色保留一個靜態變量,以便它不會每次都創建一個新實例。 – NSResponder 2012-01-11 09:45:04

+0

是的好思想@NSResponder從來沒有想過的:) – 2012-01-11 09:52:09

+2

我不想在代碼中做到這一點。我想在界面構建器中設置。 – Oksana 2012-01-13 06:45:14

-2

這是UIColor的命名顏色類別的一個非常簡單的實現。有了這個代碼在你的項目中,UIColor會記住你要保存的所有顏色,並讓你訪問你自己的顏色或系統顏色使用+colorWithName:

@interface UIColor (namedColors) 

+ (UIColor *) colorWithName:(NSString *) name; 
+ (void) setColor:(UIColor *) color forName:(NSString *) name; 

@end 

static NSMutableDictionary *colorStorage; 

@implementation UIColor (namedColors) 

+ (NSMutableDictionary *) colorStorage 
{ 
    if (!colorStorage) 
    colorStorage = [[NSMutableDictionary alloc] initWithCapacity:10]; 
    return colorStorage; 
} 

+ (UIColor *) colorWithName:(NSString *) name 
{ 
    UIColor *result =[[self colorStorage] valueForKey:name]; // See if we have a color with this name in the colorStorage. 

    if (result) return result; 

    SEL selector = NSSelectorFromString(name); // look for a class method whose selector matches the given name, such as "blueColor" or "clearColor". 

    if ([self respondsToSelector:selector] && (result = [self performSelector:selector])) 
    if ([result isKindOfClass:[self class]]) 
     return result; 

    return nil; 
} 

+ (void) setColor:(UIColor *) color forName:(NSString *) name 
{ 
    [[self colorStorage] setValue:color forKey:name]; 
} 

@end 
+0

也許太複雜了?簡單的'[UIColor myRedColor]'或'[UIColor anotherRedColor]'就足夠簡單了,不是嗎? – Tricertops 2013-08-11 16:31:30

3

是的,你可以做到這一點。

在Interface Builder的顏色選擇器彈出框的底部,有一排正方形可用於存儲顏色供以後使用。從顏色選擇器頂部顯示當前顏色的矩形中拖出一個顏色存儲它,然後稍後單擊存儲的顏色以使用它。

diagram showing how to save colors

+1

當您決定更改顏色時,這將不允許您重新塗抹顏色。但這是保存顏色以供手動重複使用的唯一原始方式。 – Tricertops 2013-08-11 16:26:52

+0

@iMartin你說得對,這是一個問題,我沒有考慮它。但是,使用這種方法時,仍然可以在全局範圍內交換顏色。如果要在故事板中更改顏色,只需在Storyboard的底層XML(或項目中的所有XIB)中查找並替換代表該顏色的所有標記。這是一種黑客攻擊,但它會起作用,而且這不是你需要經常做的事情。 – 2013-08-12 10:27:45

7

在Interface Builder 不可能的。在代碼中執行此操作,例如通過創建按鈕的特殊子類。

您可以使用系統調色板來保存顏色,但是您仍然需要在每次決定更改時將其應用於所有按鈕。或者您可以在顏色選擇器中使用「最近使用的顏色」,但這兩種方式都不夠動態。

+0

謝謝你真的回答了這個問題。 – KPM 2015-03-09 13:54:54

1

不幸的是我不相信有一種方法可以完全在界面生成器中完成。但是,你可以用一點代碼來貼近。我發現能夠改變整個應用程序的顏色在一個最好的方式是繼承您要的顏色(的UILabel,例如)的項目在初始化設置顏色:

@interface HuedUILabel : UILabel 
@end 

@implementation HuedUILabel 

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder:aDecoder]; 
    if (self) { 
     // Initialization code 
     self.textColor = [AppConfig primaryColor]; 
    } 
    return self; 
} 

@end 

然後,將標籤設置爲在IB自定義類:

現在,當你想改變你所有的UILabels的顏色,你可以通過改變你的一種顏色高清做到這一點,你不這樣做不得不用一堆外觀分配來混淆你的代碼。

相關問題