I'm trying to add gradient to an UILabel in a collectionViewCell. To add gradient I'm using this function:
extension UIView
{
func gradient(colors: [Any], startPoint: CGPoint, endPoint: CGPoint, opacity: Float, location: [NSNumber]?) {
let gradientLayer = CAGradientLayer()
gradientLayer.frame = bounds
gradientLayer.colors = colors
gradientLayer.startPoint = startPoint
gradientLayer.endPoint = endPoint
gradientLayer.opacity = opacity
gradientLayer.locations = location
layer.addSublayer(gradientLayer)
}
}
and in my collectionViewCell :
@IBOutlet weak var discount: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
discount.gradient(colors: [green,blue], startPoint: CGPoint(x: 0.0, y: 0.5), endPoint: CGPoint(x: 1.0, y: 0.5), opacity: 1, location: [0.5,0.5])
}
But it's not showing anything. Adding shadow works fine but adding gradient not showing anything. Why is that happen?