如何更改 UIView 中所有文本的文本颜色? [英] How to change the text color of all text in UIView?

查看:43
本文介绍了如何更改 UIView 中所有文本的文本颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 iOS 应用,该应用具有 2 个主题(深色和浅色),其中背景会改变颜色.

I'm building an iOS app that features 2 themes (dark and light) where the background changes colours.

我现在的问题是文字颜色的变化.如何将所有标签的文本颜色设置为 lightTextColor?

My problem now is the change of the text colour. How can I set the text colour of all labels to lightTextColor?

这是我改变颜色的地方:

This is where I change the colours:

- (void)changeColor {
    NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
    NSString *themeSetting = [standardDefaults stringForKey:@"themeKey"];
    if ([themeSetting isEqualToString:@"lightTheme"]) {
        self.view.backgroundColor = [UIColor whiteColor];
    } else {
        self.view.backgroundColor = [UIColor blackColor];
    }
}

文字颜色的变化必须以某种方式进入......

The text colour change has to get in there somehow...

推荐答案

遍历 self.view.subviews 中的所有 UIView's 并检查它是否为 UILabel 类型.如果是,则将视图投射到标签并设置颜色.

Loop through all the UIView's in self.view.subviews and check if it's of type UILabel. If it is, cast the view to a label and set the color.

- (void)changeColor {
    NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
    NSString *themeSetting = [standardDefaults stringForKey:@"themeKey"];
    if ([themeSetting isEqualToString:@"lightTheme"]) {
        self.view.backgroundColor = [UIColor whiteColor];
    } else {
        self.view.backgroundColor = [UIColor blackColor];

        //Get all UIViews in self.view.subViews
        for (UIView *view in [self.view subviews]) {
            //Check if the view is of UILabel class
            if ([view isKindOfClass:[UILabel class]]) {
                //Cast the view to a UILabel
                UILabel *label = (UILabel *)view;
                //Set the color to label
                label.textColor = [UIColor redColor];
            }
        }

    }
}

这篇关于如何更改 UIView 中所有文本的文本颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆