将CGGradient作为子图层添加到UILabel会隐藏标签文本

时间:2011-01-31 11:07:21

标签: iphone uilabel

我想将渐变添加为标签的背景。我使用以下代码来实现这一点。但问题是虽然渐变颜色出现在标签上,但文字不可见。请帮忙

lblPatientDetail.text=PatientsDetails;

lblPatientDetail.textColor=[UIColor blackColor];  

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = lblPatientDetail.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];  

[lblPatientDetail.layer addSublayer:gradient];

lblPatientDetail.backgroundColor=[UIColor clearColor];

5 个答案:

答案 0 :(得分:20)

将子图层插入UILabel会隐藏文本,因此获得所需内容的最佳方法是将标签和渐变图层添加到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];

答案 1 :(得分:2)

UIVabel内部的UILabel的建议答案有效。显然UILabels在为背景提供渐变色背景后不能在其中包含文本...不知道为什么......

但是下面是解决方案的完整代码....希望这有助于某人:)

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

快乐编码!!!!

答案 2 :(得分:1)

还有一个想法来纠正答案。 如果您使用自动布局来放置自定义视图,则可以解决此问题 - http://prntscr.com/5tj7bx

因此,您的视图与subView - UILabel和subLayer - 渐变图层的大小不同。

我通过添加一个方法来解决这个问题

class wResultView: UIView {
var label = UILabel()
var gradientLayer = CAGradientLayer()

override func layoutSubviews() {
    gradientLayer.frame = self.bounds
    label.frame = self.bounds
}
......

答案 3 :(得分:-1)

您可以在UILabel中添加UILabel 这样。

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)
    }
}

答案 4 :(得分:-4)

我认为你可以通过调整上面代码中的alpha值来创建渐变颜色。

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

我能够通过这样做获得渐变效果。