How to set gradient color to the background of UILabel in iPhone

随声附和 提交于 2019-12-08 02:34:30

问题


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

回答1:


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



回答2:


So I was attempting to do this also! I finally made it work by just placing a UIView behind the label and changing the label background color to clear. I did this in a storyboard.



来源:https://stackoverflow.com/questions/15382366/how-to-set-gradient-color-to-the-background-of-uilabel-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!