2013-06-05 47 views
5

我想要更改所有UILabel的文本顏色的總應用程序如何才能做到這一點。有任何簡單的方法來更改標籤顏色。更改整個項目的UILabel的文本顏色

我想這一點,但我想改變編寫代碼的每類

for(UIView *view in [testScroll subviews]) 
    { 
     if([view isKindOfClass:[UILabel class]]) 
     { 
      [(UILabel *)view setTextColor:[UIColor whiteColor]]; 

     } 
     else if([view isKindOfClass:[UIView class]]) 

    } 

不要任何人有簡單的方法。

+1

您針對的是iOS 5+嗎? – Alladinian

+0

@ Alladinian yes – user2197875

回答

1

創建UILabel的子類。您可以覆蓋可以設置所需文本顏色的init函數。在整個項目中使用該子類。

+0

爲什麼要在使用'[[UILabel appearance] setTextColor:[UIColor redColor]];'?這將改變所有UILabels的文本顏色。請你能解釋一下,並給出一個理由,使用'appearance'來完成你的工作嗎? – Popeye

+0

@Popeye在UILabel上使用UIAppearence代理似乎存在一些問題。請參閱http://stackoverflow.com/a/11839198/1029360 – Adithya

+0

@Popeye我無法使用ur語句獲得redcolor標籤 – user2197875

12

好吧,如果你的目標iOS 5以上,你能做到這一點很容易(你內心的appdelegate):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    //... 

    //This color will affect every label in your app 
    [[UILabel appearance] setTextColor:[UIColor redColor]]; 

    //... 

    return YES; 
} 

這工作,因爲UILabel符合UIAppearance協議,所以你可以定製符合任何一類像這樣的協議。有關此here is a nice tutorial to get you startedhere is Apple's docs on the protocol

+0

不錯,簡單的回答:) – samir

+1

藍色更漂亮:'[[UILabel外觀] setColor:[UIColor blueColor]];'^^ – Groot

+0

改變主意:'[[UILabel appearance] setColor:[UIColor purpleColor]];' – Groot

2
@interface CustomLabel : UILabel 
@end 

@implementation CustomLabel 

- (id)init{ 
    if ([super init]){ 
     self.textColor = // Putt here the color you want. 
    } 

    return self; 
} 

,現在更多信息,強制使用CustomLabel。

+0

可我的appdelegate文件更改工作 – user2197875

+0

爲什麼要這麼做時,你可以使用[[外觀的UILabel] setTextColor:[的UIColor redColor] ;?這將改變所有UILabels的文本顏色。能不能請您解釋一下,給一個理由這樣做你在使用的外觀呢? – Popeye

+1

@Popeye因爲UIAppareance做不靈活,試想如果有一天他想一個標籤將我們redColor和所有其它的藍色:) – samir