Skip to content

Instantly share code, notes, and snippets.

@nitin-agam
Created May 24, 2020 04:02
Show Gist options
  • Save nitin-agam/acea831a3a22fceb751b49198cc11a21 to your computer and use it in GitHub Desktop.
Save nitin-agam/acea831a3a22fceb751b49198cc11a21 to your computer and use it in GitHub Desktop.
extension UIView {
class func gradientViewWithText(text: String, size: CGSize, firstColor: UIColor, secondColor: UIColor, font: UIFont) -> UIView {
let view = UIView()
let layer = CAGradientLayer()
layer.colors = [firstColor.cgColor, secondColor.cgColor]
layer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
view.layer.addSublayer(layer)
let textLabel = UILabel()
textLabel.font = font
textLabel.textAlignment = .center
textLabel.text = text
textLabel.frame = layer.frame
view.addSubview(textLabel)
view.mask = textLabel
return view
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment