Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何更改UIView中所有文本的文本颜色?_Ios_Objective C_Xcode_Textcolor - Fatal编程技术网

Ios 如何更改UIView中所有文本的文本颜色?

Ios 如何更改UIView中所有文本的文本颜色?,ios,objective-c,xcode,textcolor,Ios,Objective C,Xcode,Textcolor,我正在构建一个iOS应用程序,它有两个主题(黑暗和光明),背景会改变颜色 我现在的问题是文字颜色的变化。如何将所有标签的文本颜色设置为lightTextColor 这是我改变颜色的地方: - (void)changeColor { NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults]; NSString *themeSetting = [standardDefaults stringFor

我正在构建一个iOS应用程序,它有两个主题(黑暗和光明),背景会改变颜色

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

这是我改变颜色的地方:

- (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];
    }
}

文本颜色更改必须以某种方式进入其中…

self.view.subview
中循环所有
ui视图
,并检查其是否为UILabel类型。如果是,请将视图强制转换为标签并设置颜色

- (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];
            }
        }

    }
}