UIStackView 中的 UILabel 中的 CAGradientLayer

CAGradientLayer in UILabel in UIStackView

这是 tableHeaderView 的自定义 UIView 子类。

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

- (instancetype)initWithTitle:(NSString *)title subtitle:(NSString *)subtitle colors:(NSArray<UIColor *> *)colors {
    if (self = [super initWithFrame:CGRectMake(0, 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 的框架变为 {[width of subtitleLabel], 0, 0, 0} 没原因。原因是渐变视图,因为没有它它工作正常,但我找不到为什么。

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

编辑: 工作代码:

- (instancetype)initWithTitle:(NSString *)title subtitle:(NSString *)subtitle colors:(NSArray<__kindof UIColor *> *)colors {
    if (self = [super initWithFrame:CGRectMake(0, 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] init];
        [gradientView.layer addSublayer:gradient];
        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],

            [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],

    ]];