I am trying to use a 3-color gradient to color the text in Xcode and it would seem impossible to get the results I'm looking for. I had success with the following, but it only gives me two colors through the gradient.
@IBOutlet weak var textSample: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
textSample.textColor = UIColor(patternImage: gradientImage(size: textSample.frame.size, color1: CIColor(color: UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)), color2: CIColor(color: UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.2))))
}
func gradientImage(size: CGSize, color1: CIColor, color2: CIColor) -> UIImage {
let context = CIContext(options: nil)
let filter = CIFilter(name: "CILinearGradient")
var startVector: CIVector
var endVector: CIVector
filter!.setDefaults()
startVector = CIVector(x: size.width * 0.5, y: 0)
endVector = CIVector(x: size.width * 0.5, y: size.height * 0.8)
filter!.setValue(startVector, forKey: "inputPoint0")
filter!.setValue(endVector, forKey: "inputPoint1")
filter!.setValue(color1, forKey: "inputColor0")
filter!.setValue(color2, forKey: "inputColor1")
let image = UIImage(cgImage: context.createCGImage(filter!.outputImage!, from: CGRect(x: 0, y: 0, width: size.width, height: size.height))!)
return image
}
What I would like to do is have 3 places with three colors:
location1: y:0.0
location2: y:0.8
location3: y:1.0
color1: UIColour(red: 1, green: 1, blue: 1, alpha: 0.2)
color2: UIColour(red: 1, green: 1, blue: 1, alpha: 1.0)
color3: UIColour(red: 1, green: 1, blue: 1, alpha: 0.45)
I am trying to simplify this by simply adding this gradient with three locations to UIView, but it seems that no matter what I do to mask this UIView with UILabel, nothing works. Any suggestions would be very helpful. I have attached an image with what I received with my code above, and an example of what I would like to achieve, if possible.