如何为所有子视图设置文本颜色?
How to set text color for all subviews?
例如,我创建了一个 UIView
,其中有一些 UILabel
和 UIButton
子视图。所有的子视图都有不同的文本颜色。
有没有一种简单的方法可以为所有子视图设置文本颜色并取消它?它应该像面具一样工作?
您可以运行在视图的子视图上循环
for (UIView *view in yourView.subviews)
{
if([view isKindOfClass:[UILabel class]] )
{
UILabel *label = (UILabel*)view;
label.textColor = [UIColor greenColor];
}
//Similarly you can check and change for UIButton , All UI Elements
}
对于UIButton
,如果它们是默认类型(.RoundedRectType),只需将tintColor
属性设置为您想要的他们的超级视图。对于 UILabel
不幸的是,这还不够,但您可以子类化 UILabel
并覆盖 -tintColorDidChange
方法,如下所示:
// In MyLabelSubclass.m
- (void)tintColorDidChange {
self.textColor = self.tintColor;
}
// Swift version
override func tintColorDidChange {
textColor = tintColor
}
有关为什么 UILabel
在 tintColor
更改时不自动更新它的 textColor
的更多信息,this answer 很好地解释了正在发生的事情以及这一技术选择背后的原因。
您可以通过以下任一方式完成:
- 通过在
UILabel
中设置UIAppearance
,在AppDelegate
中设置UIButton
class,check link。或者
- 通过创建
UILabel
和 UIButton
的子 class。
我更喜欢第一个:
- (BOOL)application : (UIApplication *)application didFinishLaunchingWithOptions : (NSDictionary *)launchOptions
{
//This color will affect every label in your app
[[UIButton appearance] setTintColor:[UIColor redColor]];
[[UILabel appearance] setTextColor:[UIColor redColor]];
return YES;
}
你得到所有的子视图,然后你根据它们的类型投射它们
之后你改变他们的颜色
let subviews = view.subviews
for v in subviews{
if v is UILabel {
let currentLabel = v as! UILabel
currentLabel.textColor = UIColor.white
} else if v is UIButton {
let currentButton = v as! UIButton
currentButton.setTitleColor(UIColor.white, for:.normal)
}
}
这里我将 UIbuttons 和 UILabels 的颜色都改成了白色
例如,我创建了一个 UIView
,其中有一些 UILabel
和 UIButton
子视图。所有的子视图都有不同的文本颜色。
有没有一种简单的方法可以为所有子视图设置文本颜色并取消它?它应该像面具一样工作?
您可以运行在视图的子视图上循环
for (UIView *view in yourView.subviews)
{
if([view isKindOfClass:[UILabel class]] )
{
UILabel *label = (UILabel*)view;
label.textColor = [UIColor greenColor];
}
//Similarly you can check and change for UIButton , All UI Elements
}
对于UIButton
,如果它们是默认类型(.RoundedRectType),只需将tintColor
属性设置为您想要的他们的超级视图。对于 UILabel
不幸的是,这还不够,但您可以子类化 UILabel
并覆盖 -tintColorDidChange
方法,如下所示:
// In MyLabelSubclass.m
- (void)tintColorDidChange {
self.textColor = self.tintColor;
}
// Swift version
override func tintColorDidChange {
textColor = tintColor
}
有关为什么 UILabel
在 tintColor
更改时不自动更新它的 textColor
的更多信息,this answer 很好地解释了正在发生的事情以及这一技术选择背后的原因。
您可以通过以下任一方式完成:
- 通过在
UILabel
中设置UIAppearance
,在AppDelegate
中设置UIButton
class,check link。或者 - 通过创建
UILabel
和UIButton
的子 class。
我更喜欢第一个:
- (BOOL)application : (UIApplication *)application didFinishLaunchingWithOptions : (NSDictionary *)launchOptions
{
//This color will affect every label in your app
[[UIButton appearance] setTintColor:[UIColor redColor]];
[[UILabel appearance] setTextColor:[UIColor redColor]];
return YES;
}
你得到所有的子视图,然后你根据它们的类型投射它们 之后你改变他们的颜色
let subviews = view.subviews
for v in subviews{
if v is UILabel {
let currentLabel = v as! UILabel
currentLabel.textColor = UIColor.white
} else if v is UIButton {
let currentButton = v as! UIButton
currentButton.setTitleColor(UIColor.white, for:.normal)
}
}
这里我将 UIbuttons 和 UILabels 的颜色都改成了白色