1

For example, the left side of the label is gray and the right side is blue. I want the color to be gray on the left side, but become darker as it goes right until the right side of the label is completely blue. Is there a way to do this without looking for an image online and setting the background color of the label to be the image?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jon
  • 11
  • 1
  • 2
  • Maybe something like [How to Apply Gradient to background view of iOS Swift App](https://stackoverflow.com/questions/24380535/how-to-apply-gradient-to-background-view-of-ios-swift-app) – MadProgrammer Jul 26 '18 at 04:30
  • Is there a way to make the gradient left to right instead of down to up? – Jon Jul 26 '18 at 20:35
  • [Google is your friend](https://www.google.com/search?client=safari&rls=en&q=CAGradientLayer&ie=UTF-8&oe=UTF-8); always start with the offical docs; look at other possible [examples](https://medium.com/@arb1nsnmgl/playing-with-cagradientlayer-swift-3-0-6230b00054be) - your question isn't that unique and has been done before – MadProgrammer Jul 26 '18 at 21:07

1 Answers1

0

You can add UILabel into the UIView and add Gradient Layer to that UIView. For add Gradient Layer you can use following code.

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)

You can also refer this question

girish_pro
  • 838
  • 9
  • 18
  • I used this to add a gradient to a UINavigationBar, but the gradient covers my UILabel in the navigation bar. How can I add the gradient without it covering my buttons? – Jon Jul 26 '18 at 20:41