Search code examples
objective-ccalayeruistackview

CAGradientLayer in UILabel in UIStackView


This is a custom UIView subclass for a tableHeaderView.

I'm currently trying to put a gradient to an UILabel that's in a vertical UIStackView. It works really well without gradient even in RTL languages (gets moved to the right), but as soon as I add the gradient, the label that's the maskView of the gradientView gets completely out of its place. Here the code for better comprehension:

- (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;
}

With LTR languages, it looks good, but with RTL languages, the frame of the gradientView that contains the titleLabel becomes {[width of subtitleLabel], 0, 0, 0} for no reason. The cause is the gradient view, because without it it works fine, but I can't find why.

I tried titleLabel.layer.mask, placing the gradient block at the end, but nothing worked.

EDIT : Working code:

- (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;
}

Solution

  • The problem is that your gradientView has no intrinsic content size.

    Try changing your constraints to this:

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