I am setting up an array that has a transition throughout he colors of the rainbow. Right now I\'ve just manually entered the colors in the array but there are too many to m
A Swift 5, iOS 13 update to BJHomer answer
extension UIButton {
func rainbowText() {
var colors:[UIColor] = []
let increment:CGFloat = 0.02
for hue:CGFloat in stride(from: 0.0, to: 1.0, by: increment) {
let color = UIColor(hue: hue, saturation: 1.0, brightness: 1.0, alpha: 1.0)
colors.append(color)
}
var colorIndex = 0
Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { (timer) in
if colorIndex < colors.count {
self.setTitleColor(colors[colorIndex], for: .normal)
colorIndex = colorIndex + 1
} else {
self.setTitleColor(colors[0], for: .normal)
timer.invalidate()
}
}
}
}
You call it like this ...
buttonOutlet.rainbowText()