gpt4 book ai didi

iphone - 将 CGGradient 添加为 UILabel 的子图层会隐藏标签的文本

转载 作者:行者123 更新时间:2023-12-03 18:19:17 24 4
gpt4 key购买 nike

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

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

最佳答案

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

关于iphone - 将 CGGradient 添加为 UILabel 的子图层会隐藏标签的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4850149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com