在UIScrollView中更改每个UILabel的颜色

kop*_*ion 2 objective-c uilabel

是否有可能遍历UILabel我的所有s UIScrollview并改变颜色?

Noa*_*oon 7

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

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

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

- (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];
Run Code Online (Sandbox Code Playgroud)