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

use*_*875 5 objective-c textcolor uilabel

我想改变所有UILabel的文本颜色为总app我怎么能这样做.是否有任何简单的方法来改变标签颜色.

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

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

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

    }
Run Code Online (Sandbox Code Playgroud)

有没有人有简单的方法.

All*_*ian 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;
}
Run Code Online (Sandbox Code Playgroud)

这是因为UILabel符合UIAppearance协议,所以你可以自定义任何符合协议的类.有关这方面的更多信息,这是一个很好的教程,可以帮助您入门,这是Apple关于协议的文档


Adi*_*hya 1

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