【问题标题】:How to set gradient color to the background of UILabel in iPhone如何在iPhone中为UILabel的背景设置渐变色
【发布时间】:2023-03-09 05:06:01
【问题描述】:

我正在使用以下代码将渐变颜色设置为标签的背景,但没有效果我做错了什么? 代码在这里:

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

【问题讨论】:

    标签: uilabel cagradientlayer


    【解决方案1】:

    当我将 CAGradientLayer 插入主视图时,它工作正常。

    [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】:

      所以我也尝试这样做!我终于通过在标签后面放置一个 UIView 并将标签背景颜色更改为清除来使其工作。我在情节提要中做到了这一点。

      【讨论】:

      • 您能否详细说明您是如何做到这一点的
      • 因此尝试在 UILabel 上执行此操作会出现问题。为了避免这种情况,我只是在 UILabel 后面显示了一个标准 UIView,并使 UILabel 背景透明,以便您可以看到更容易放置在该 uiview 中的渐变。对于视图的放置,我相信我使用了 xib,只是在代码中引用了它们,并使用了类似上面的代码@Nyakiba
      猜你喜欢
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      • 2019-09-04
      • 2012-06-25
      • 2020-09-24
      • 1970-01-01
      • 1970-01-01
      • 2012-06-16
      相关资源
      最近更新 更多