为整个项目更改UILabel的文本颜色

时间:2013-06-05 10:06:46

标签: objective-c uilabel textcolor

我想更改所有UILabel的文本颜色以获得全部应用程序我该怎么做。是否有任何简单的方法来更改标签颜色。

我试过这个,但我想改变在每个类中编写这段代码

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

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

    }

有没有人有简单的方法。

3 个答案:

答案 0 :(得分: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

的详细信息,请参阅

答案 1 :(得分:2)

@interface CustomLabel : UILabel
 @end

 @implementation CustomLabel

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

   return self;
 }

现在jus使用你的CustomLabel。

答案 2 :(得分:1)

创建UILabel的子类。您可以覆盖init函数,您可以在其中设置所需的文本颜色。在整个项目中使用该子类。