【问题标题】:How to apply a color gradient to the background of a UILabel? [duplicate]如何将颜色渐变应用于 UILabel 的背景? [复制]
【发布时间】:2019-01-02 23:43:04
【问题描述】:

例如,标签左侧为灰色,右侧为蓝色。我希望左侧的颜色为灰色,但随着颜色向右变暗,直到标签的右侧完全为蓝色。有没有办法做到这一点,而无需在线查找图像并将标签的背景颜色设置为图像?

【问题讨论】:

标签: ios swift uilabel gradient


【解决方案1】:

您可以将 UILabel 添加到 UIView 并将渐变层添加到该 UIView。 要添加渐变层,您可以使用以下代码。

let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 50))
let gradient = CAGradientLayer()

gradient.frame = view.bounds
gradient.colors = [UIColor.white.cgColor, UIColor.black.cgColor]

view.layer.insertSublayer(gradient, at: 0)

你也可以参考这个question

【讨论】:

  • 我用它给 UINavigationBar 添加了渐变,但是渐变覆盖了导航栏中的 UILabel。如何在不覆盖按钮的情况下添加渐变?
猜你喜欢
  • 2012-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多