2014-08-30 2 views
2

배경이 색이 바뀌는 2 가지 테마 (어둡거나 밝은)가있는 iOS 앱을 만들고 있습니다.UIView에서 모든 텍스트의 텍스트 색상을 변경하는 방법은 무엇입니까?

이제 내 문제는 텍스트 색상이 변경되었습니다. 모든 라벨의 텍스트 색상을 lightTextColor으로 설정하려면 어떻게해야합니까?

나는 색상을 변경하는 곳이다 : 그것은 유형의 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]; 
    } 
} 

텍스트 색상 변경은 self.view.subviews의 모든 UIView's을 통해 ... 어떻게 든 거기에

답변

6

루프를 얻을 확인한다 . 그럴 경우 뷰를 레이블로 캐스트하고 색상을 설정하십시오.

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

    } 
} 
+0

감사합니다. 이 작품 –

관련 문제