将 CGGradient 添加为 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];
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 the text is not visible. please help
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
向 UILabel 插入子图层会隐藏文本,因此获得所需内容的最佳方法是将标签和渐变图层添加到 UIView。
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 中使用 UILabel 的建议答案是有效的。显然,在给背景提供渐变颜色背景后,UILabels 不能在其中包含文本...不知道为什么...
但是这是解决方案的完整代码...希望这对某人有帮助:)
快乐编码! !
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 :)
Happy coding!!!!
还要再思考一下正确答案。
如果您使用自动布局来放置自定义视图,您可能会遇到此问题 - http://prntscr.com/5tj7bx
因此,您的视图的大小与 subView - UILabel 和 subLayer - 渐变层不同。
我通过添加一种方法解决了这个问题
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
最简单的解决方案。
这是苹果的一个巨大错误,他们从来没有修复过20个?年。
要解决此问题,只需在顶部添加另一个 UILabel 并传递“.text”设置即可。
如果有必要,您还可以“传递”例如字体、文本颜色等。
就是这么简单。 TrickLabel 的工作方式与 UILabel 完全相同,您可以像平常一样说
.text = "blah"
等。Simplest solution.
It's a huge mistake by Apple, they have never fixed for 20? years.
To work around, simply add another UILabel on top and pass on the ".text" setting.
If necessary, you can also "pass along" eg the font, text color etc.
It's that easy. TrickLabel works exactly like a UILabel, you can say
.text = "blah"
etc as normal.意味着创建 UIView 的解决方案对于常规使用来说不太方便。让我为此建议一个可重用的解决方案。
The solution which implies creation of UIView is not very convenient for regular use. Let me suggest a reusable solution for this.
您可以在
UILabel
中添加UILabel
像这样。
You can add
UILabel
withinUILabel
Like this.
我认为你可以通过调整上面代码中的 alpha 值来创建渐变颜色。
通过这样做我能够获得渐变效果。
I think you can create a gradient color by adjusting the alpha value in the above code.
I was able to get a gradient effect by doing so.