如何在 iPhone 中为 UILabel 的背景设置渐变颜色
问题描述:
我正在使用以下代码将渐变颜色设置为标签的背景,但没有影响我做错了什么?代码在这里:
I am using the following code to set the gradient color to the background of label but no effect what i am doing wrong? Code is here:
[self.teamName setTextColor:[UIColor whiteColor]];
[self.teamName setBackgroundColor:[UIColor clearColor]];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.teamName.bounds;
gradientLayer.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor]CGColor], (id)[[UIColor blackColor]CGColor], nil];
[self.teamName.layer insertSublayer:gradientLayer atIndex:0];
答
当我将 CAGradientLayer 插入到主视图时它工作正常.
It working fine when i insert the CAGradientLayer to the main view.
[self.teamName setTextColor:[UIColor whiteColor]];
[self.teamName setBackgroundColor:[UIColor clearColor]];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.teamName.bounds;
gradientLayer.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor]CGColor], (id)[[UIColor blackColor]CGColor], nil];
[self.view.layer insertSublayer:gradientLayer atIndex:0];