在UIScrollView中更改每个UILabel的颜色

时间:2011-08-31 00:19:40

标签: objective-c uilabel

是否可以循环浏览UILabel中的所有UIScrollview并更改颜色?

2 个答案:

答案 0 :(得分:7)

假设它们是滚动视图的所有子视图(第一级子视图),而不是其中的容器视图......

for(UIView *subview in theScrollView.subviews)
    if([subview isKindOfClass:[UILabel class]])
        [(UILabel *)subview setTextColor:[UIColor whateverColor]];

如果标签位于滚动视图内的其他视图中,则必须递归到每个子视图中并执行相同的操作,但这是从上面开始的一个非常简单的步骤。例如:

- (void)recolorLabelSubviews:(UIView *)view
{
    for(UIView *subview in view.subviews)
    {
        if([subview isKindOfClass:[UILabel class]])
            [(UILabel *)subview setTextColor:[UIColor whateverColor]];
        else
            [self recolorLabelSubviews:subview];
        // this doesn't handle the case where you have a label as a subview of a label
        // if for some reason you're doing that, just move the [self recolorEtc:] call out of the "else" block
    }
}

// then, wherever you want to recolor every label in the scroll view...

[self recolorLabelSubviews:theScrollView];

答案 1 :(得分:0)

for (UILabel *lbl in [self.view subviews]) {
    if([lbl isKindOfClass:[UILabel class]])
    {
        lbl.backgroundColor=[UIColor yellowColor];
    }
}