微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

UIStackView 中 UILabel 中的 CAGradientLayer

如何解决UIStackView 中 UILabel 中的 CAGradientLayer

这是 tableHeaderView 的自定义 UIView 子类。

我目前正在尝试为垂直 UIStackView 中的 UILabel 设置渐变。即使在 RTL 语言中,它在没有渐变的情况下也能很好地工作(向右移动),但是一旦我添加了渐变,渐变视图的 maskView 标签就完全脱离了它的位置。这里是更好理解的代码

- (instancetype)initWithTitle:(Nsstring *)title subtitle:(Nsstring *)subtitle colors:(NSArray<UIColor *> *)colors {
    if (self = [super initWithFrame:CGRectMake(0,self.superview.frame.size.width,200)]) {
        // Labels
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = title;
        titleLabel.font = [UIFont boldSystemFontOfSize:42.f];
        titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [titleLabel sizetoFit];

        UILabel *subtitleLabel = [[UILabel alloc] init];
        subtitleLabel.text = subtitle;
        subtitleLabel.font = [UIFont systemFontOfSize:24.f];
        subtitleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [subtitleLabel sizetoFit];

        // Create gradient
        CAGradientLayer *gradient = [CAGradientLayer layer];
        gradient.frame = titleLabel.bounds;
        NSMutableArray *cgcolors = [NSMutableArray new];
        for (int i = 0; i < colors.count; i++) {
            cgcolors[i] = (id)colors[i].CGColor;
        }
        gradient.colors = cgcolors;
        gradient.locations = @[@0,@1];
        
        UIView *gradientView = [[UIView alloc] initWithFrame:titleLabel.bounds];
        [gradientView.layer addSublayer:gradient];
        [gradientView addSubview:titleLabel];
        gradientView.maskView = titleLabel;

        // Stack
        UIStackView *stackView = [[UIStackView alloc] initWithArrangedSubviews:@[gradientView,subtitleLabel]];
        stackView.alignment = UIStackViewAlignmentLeading;
        stackView.axis = UILayoutConstraintAxisvertical;
        stackView.distribution = UIStackViewdistributionEqualCentering;
        stackView.spacing = 0;
        stackView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:stackView];

        [NSLayoutConstraint activateConstraints:@[
            [stackView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:25],[stackView.centerYAnchor constraintEqualToAnchor:self.centerYAnchor],[stackView.heightAnchor constraintEqualToConstant:titleLabel.frame.size.height + subtitleLabel.frame.size.height]
        ]];
    }

    return self;
}

使用LTR语言,看起来不错,但是使用RTL语言,包含titleLabel的gradientView的frame无缘无故变成了{[width of subtitleLabel],0}。原因是渐变视图,因为没有它它可以正常工作,但我找不到原因。

我尝试了 titleLabel.layer.mask,将渐变块放在最后,但没有任何效果

编辑: 工作代码

- (instancetype)initWithTitle:(Nsstring *)title subtitle:(Nsstring *)subtitle colors:(NSArray<__kindof UIColor *> *)colors {
    if (self = [super initWithFrame:CGRectMake(0,@1];

        UIView *gradientView = [[UIView alloc] init];
        [gradientView.layer addSublayer:gradient];
        gradientView.maskView = titleLabel;

        // Stack
        UIStackView *stackView = [[UIStackView alloc] initWithArrangedSubviews:@[gradientView,[stackView.heightAnchor constraintEqualToConstant:titleLabel.frame.size.height + subtitleLabel.frame.size.height],[gradientView.widthAnchor constraintEqualToConstant:titleLabel.frame.size.width],[gradientView.heightAnchor constraintEqualToConstant:titleLabel.frame.size.height]
        ]];
    }

    return self;
}

解决方法

问题在于您的 gradientView 没有内在的内容大小。

尝试将您的约束更改为:

    [NSLayoutConstraint activateConstraints:@[
        [stackView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:25],[stackView.centerYAnchor constraintEqualToAnchor:self.centerYAnchor],// shouldn't need to set a height anchor on the stackView
        //[stackView.heightAnchor constraintEqualToConstant:titleLabel.frame.size.height + subtitleLabel.frame.size.height],[gradientView.widthAnchor constraintEqualToConstant:titleLabel.frame.size.width],[gradientView.heightAnchor constraintEqualToConstant:titleLabel.frame.size.height],]];

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。