Adding a CGGradient as sublayer to UILabel hides the text of label

前端 未结 5 1099
青春惊慌失措
青春惊慌失措 2020-12-03 17:17

I want to add the gradient as a background to label. I used the following code to acheive that. but the problem is that though the gradient color appears on the label, but t

相关标签:
5条回答
  • 2020-12-03 17:25

    I think you can create a gradient color by adjusting the alpha value in the above code.

    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor],(id)[[UIColor colorWithRed:255/255.0 green:239/255.0 blue:215/255.0 alpha:0.1] CGColor],nil];
    

    I was able to get a gradient effect by doing so.

    0 讨论(0)
  • 2020-12-03 17:30

    You can add UILabel within UILabel Like this.

    class GradientLabel: UILabel {  
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
    
            self.clipsToBounds = true
            DispatchQueue.main.async {
                self.applyGradient(with: Color.gradientColor, gradient: GradientOrientation.topLeftBottomRight)
                self.textColor = UIColor.white
            }
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
    
                let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))
                label.text = self.text
                label.font = self.font
                label.textColor = self.textColor
                label.textAlignment = self.textAlignment
                self.text = ""
                self.addSubview(label)
            }
        }
    }
    
    extension UIView {
    
        func applyGradient(with colours: [UIColor], locations: [NSNumber]? = nil) {
            let gradient = CAGradientLayer()
            gradient.frame = self.bounds
            gradient.colors = colours.map { $0.cgColor }
            gradient.locations = locations
            self.layer.insertSublayer(gradient, at: 0)
        }
    
        func applyGradient(with colours: [UIColor], gradient orientation: GradientOrientation) {
            let gradient = CAGradientLayer()
            gradient.frame = self.bounds
            gradient.colors = colours.map { $0.cgColor }
            gradient.startPoint = orientation.startPoint
            gradient.endPoint = orientation.endPoint
            self.layer.insertSublayer(gradient, at: 0)
        }
    }
    
    0 讨论(0)
  • 2020-12-03 17:35

    The suggested answer with the UILabel inside a UIView works. Apparently UILabels cannot have text within them after giving the background a gradient color background... don't know why....

    But heres the full code for the solution.... hope this helps someone :)

    UIView *EnvironmentalsLabelView = [[UIView alloc] initWithFrame:CGRectMake(0, 300, 320, 20)];
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = EnvironmentalsLabelView.bounds;
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor darkGrayColor]CGColor], (id)[[UIColor blackColor]CGColor], nil];
    [EnvironmentalsLabelView.layer insertSublayer:gradient atIndex:0];
    [scroller addSubview:EnvironmentalsLabelView];
    
    UILabel *EnviornmentalsLabelText = [[UILabel alloc] initWithFrame:EnvironmentalsLabelView.bounds];
    [EnviornmentalsLabelText setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12.0f]];
    EnviornmentalsLabelText.textAlignment = NSTextAlignmentCenter;
    EnviornmentalsLabelText.backgroundColor = [UIColor clearColor];
    EnviornmentalsLabelText.text = @"Environmental Benefits";
    [EnvironmentalsLabelView addSubview:EnviornmentalsLabelText];
    

    Happy coding!!!!

    0 讨论(0)
  • 2020-12-03 17:36

    One more additional think to correct answer. If you are use autolayouts for place your custom view you can get this problem - http://prntscr.com/5tj7bx

    So your view has a different size then subView - UILabel and subLayer - gradient layer.

    I solve this problem by add one method

    class wResultView: UIView {
    var label = UILabel()
    var gradientLayer = CAGradientLayer()
    
    override func layoutSubviews() {
        gradientLayer.frame = self.bounds
        label.frame = self.bounds
    }
    ......
    
    0 讨论(0)
  • 2020-12-03 17:49

    Inserting a sublayer to a UILabel hides the text, so the best way to get what you want is to add the label and gradient layer to a UIView.

    UIView *gradientLabelView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = gradientLabelView.bounds;
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor],(id)[[UIColor colorWithRed:255/255.0 green:239/255.0 blue:215/255.0 alpha:1.0] CGColor],nil];
    
    [gradientLabelView.layer addSublayer:gradient];
    
    lblPatientDetail.frame = gradientLabelView.bounds;
    lblPatientDetail.backgroundColor = [UIColor clearColor];
    [gradientLabelView addSubview:lblPatientDetail];
    
    [self addSubview:gradientLabelView];
    
    0 讨论(0)
提交回复
热议问题