How to Do Programmatically Gradient Border Color Uibutton with Swift

How can I do programmatically gradient border color UIButton with Swift

let gradient = CAGradientLayer()
gradient.frame = CGRect(origin: CGPointZero, size: self.myButton.frame.size)
gradient.colors = [UIColor.blueColor().CGColor, UIColor.greenColor().CGColor]

let shape = CAShapeLayer()
shape.lineWidth = 2
shape.path = UIBezierPath(rect: self.myButton.bounds).CGPath
shape.strokeColor = UIColor.blackColor().CGColor
shape.fillColor = UIColor.clearColor().CGColor
gradient.mask = shape

self.myButton.layer.addSublayer(gradient)

Swift 3 version:

    let gradient = CAGradientLayer()
gradient.frame = CGRect(origin: CGPoint.zero, size: self.myButton.frame.size)
gradient.colors = [UIColor.blue.cgColor, UIColor.green.cgColor]

let shape = CAShapeLayer()
shape.lineWidth = 2
shape.path = UIBezierPath(rect: self.myButton.bounds).cgPath
shape.strokeColor = UIColor.black.cgColor
shape.fillColor = UIColor.clear.cgColor
gradient.mask = shape

self.myButton.layer.addSublayer(gradient)

How can I do programmatically gradient border color UIButton with Swift

let gradient = CAGradientLayer()
gradient.frame = CGRect(origin: CGPointZero, size: self.myButton.frame.size)
gradient.colors = [UIColor.blueColor().CGColor, UIColor.greenColor().CGColor]

let shape = CAShapeLayer()
shape.lineWidth = 2
shape.path = UIBezierPath(rect: self.myButton.bounds).CGPath
shape.strokeColor = UIColor.blackColor().CGColor
shape.fillColor = UIColor.clearColor().CGColor
gradient.mask = shape

self.myButton.layer.addSublayer(gradient)

Swift 3 version:

    let gradient = CAGradientLayer()
gradient.frame = CGRect(origin: CGPoint.zero, size: self.myButton.frame.size)
gradient.colors = [UIColor.blue.cgColor, UIColor.green.cgColor]

let shape = CAShapeLayer()
shape.lineWidth = 2
shape.path = UIBezierPath(rect: self.myButton.bounds).cgPath
shape.strokeColor = UIColor.black.cgColor
shape.fillColor = UIColor.clear.cgColor
gradient.mask = shape

self.myButton.layer.addSublayer(gradient)

Rounded Gradient Border on UIButton

You need to set corner radius before creating UIBezierPath with UIButton bounds and cornerRadius.

Try below:

self.myBtn.layer.cornerRadius = self.myBtn.frame.height/2
self.myBtn.clipsToBounds = true

let gradient = CAGradientLayer()
gradient.frame = CGRect(origin: CGPoint.zero, size: self.myBtn.frame.size)
gradient.colors = [UIColor.blue.cgColor, UIColor.green.cgColor]

let shape = CAShapeLayer()
shape.lineWidth = 6

shape.path = UIBezierPath(roundedRect: self.myBtn.bounds, cornerRadius: self.myBtn.layer.cornerRadius).cgPath

shape.strokeColor = UIColor.black.cgColor
shape.fillColor = UIColor.clear.cgColor
gradient.mask = shape

self.myBtn.layer.addSublayer(gradient)

Output:-

Sample Image

Creating a clear button with gradient border and gradient text

I have created a demo for you, you can do this with the help of CAGradientLayer see the following output and code for this.

Sample Image

Storyboard:

Sample Image

For gradient button text color and border put your UIButton inside UIView, then assign CAGradientLayer to UIview.

Note:- Don't forget to set the button as the views mask, See the following code.

import UIKit

class ViewController: UIViewController {

@IBOutlet var viewForButton: UIView!
@IBOutlet var myButton: UIButton!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

// Create a gradient layer
let gradient = CAGradientLayer()

// gradient colors in order which they will visually appear
gradient.colors = [UIColor.red.cgColor, UIColor.blue.cgColor]

// Gradient from left to right
gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)

// set the gradient layer to the same size as the view
gradient.frame = viewForButton.bounds

// add the gradient layer to the views layer for rendering
viewForButton.layer.insertSublayer(gradient, at: 0)

// Tha magic! Set the button as the views mask
viewForButton.mask = myButton

//Set corner Radius and border Width of button
myButton.layer.cornerRadius = myButton.frame.size.height / 2
myButton.layer.borderWidth = 5.0
}

}

Extension: You can also prefer this extension for the same.

extension UIView{

func gradientButton(_ buttonText:String, startColor:UIColor, endColor:UIColor) {

let button:UIButton = UIButton(frame: self.bounds)
button.setTitle(buttonText, for: .normal)

let gradient = CAGradientLayer()
gradient.colors = [startColor.cgColor, endColor.cgColor]
gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
gradient.frame = self.bounds
self.layer.insertSublayer(gradient, at: 0)
self.mask = button

button.layer.cornerRadius = button.frame.size.height / 2
button.layer.borderWidth = 5.0
}
}

How to use:

testView.gradientButton("Hello", startColor: .red, endColor: .blue)

custom UIButton class - gradient going out off button borders

Remove gradientLayer.frame from setup and add in layoutSubviews.

Also set self.bounds instead of layer.bounds.

override func layoutSubviews() {
super.layoutSubviews()
gradientLayer.frame = self.bounds // < Here
}

Note : Remove gradient.frame = layer.bounds from var gradientLayer. Not needed.



Related Topics



Leave a reply



Submit