5

我想更改整个应用程序的所有 UILabel 文本颜色我该怎么做。有没有简单的方法来更改标签颜色。

我试过这个,但我想改变在每个班级写这段代码

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

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

    }

有没有人有简单的方法。

4

3 回答 3

12

Well if you're targeting iOS 5+ you could do that really easy (inside you appdelegate):

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

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

    //...

    return YES;
}

This works because UILabel conforms to UIAppearance protocol, so you could customize any class that conforms to the protocol like this. For more information on this here is a nice tutorial to get you started and here is Apple's docs on the protocol

于 2013-06-05T10:17:28.550 回答
2
@interface CustomLabel : UILabel
 @end

 @implementation CustomLabel

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

   return self;
 }

现在只需使用您的 CustomLabel。

于 2013-06-05T10:18:50.610 回答
1

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

于 2013-06-05T10:13:44.723 回答