更改 UIScrollView 中每个 UILabel 的颜色

发布于 2024-12-02 05:27:54 字数 74 浏览 0 评论 0原文

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

Is it possible to loop through all the UILabels in my UIScrollview and change the color?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独自←快乐 2024-12-09 05:27:54

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

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

Assuming they’re all subviews (first-level children) of the scroll view, not of container views inside it...

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

If the labels are inside other views inside the scroll view, you’ll have to recurse into each of the subviews and do the same thing, but that’s a pretty simple step from the above. Example:

- (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];
書生途 2024-12-09 05:27:54

尝试

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

try

for (UILabel *lbl in [self.view subviews]) {
    if([lbl isKindOfClass:[UILabel class]])
    {
        lbl.backgroundColor=[UIColor yellowColor];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文