问题描述
出于某些原因,我试图在我的 UILabel
中添加渐变拼音,因为 CAGradientLayer
覆盖了我的文字。
我做错了什么
-(void)viewDidLoad {
[super viewDidLoad];
CAGradientLayer * gradient = [CAGradientLayer层];
gradient.frame = CGRectMake(0,0,myLabel.frame.size.width,myLabel.frame.size.height);
gradient.colors = myColors;
[myLabel.layer insertSublayer:gradient atIndex:0];
}
CAGradientLayer
覆盖了标签的文本,因为文本是由您通过添加子层明确覆盖的超级层的内容绘制的。
UIView 子类,您可以在其中重写
+ [UIView layerClass]
并返回 [CAGradientLayer]
。添加一个好的初始化方法来设置渐变。 下一个小狗将 UILabel
添加为自定义渐变的子视图视图。
I am trying to add a gradient layet to my UILabel
for some reasons the CAGradientLayer
covers my text.Am I doing anything wrong
- (void)viewDidLoad {
[super viewDidLoad];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(0, 0, myLabel.frame.size.width, myLabel.frame.size.height);
gradient.colors = myColors;
[myLabel.layer insertSublayer:gradient atIndex:0];
}
The CAGradientLayer
is covering the text of your label because the text is drawn by the content of the super layer that you explicitly covered by adding a sublayer.
Your easiest solution is to use two views. A UIView
subclass where you override +[UIView layerClass]
and return a [CAGradientLayer]
. Add a good init method to setup your gradient.
Next pup add the UILabel
as a subview of your custom gradient view.
这篇关于目标C-CAGradientLayer涵盖UILabel中的文本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!