Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a clear button with gradient border and gradient text

I'm trying to add a gradient to my UIButton Title and to the border of the button. I've gone through most of the solution on here which I cannot get working for the life of me, might be outdated, I'm not sure. So currently I extend the UIView in order to set the gradient of whatever. So how would I add another function for this feature?

func setGradientBackground(colorOne: UIColor, colorTwo: UIColor)  {
    let gradientlayer = CAGradientLayer()
    gradientlayer.frame = bounds
    gradientlayer.colors = [colorOne.cgColor, colorTwo.cgColor]
    gradientlayer.locations = [0, 1]
    gradientlayer.startPoint = CGPoint(x: 1.0, y: 0.0)
    gradientlayer.endPoint = CGPoint(x: 0.0, y: 0.0)

    layer.insertSublayer(gradientlayer, at: 0)

}

An Example of what I would like to achieve

like image 406
OCTAVIAN Avatar asked Mar 28 '19 14:03

OCTAVIAN


People also ask

How do you add Gradient to border in flutter?

Working with simple borders // dart Container( padding: const EdgeInsets. all(10), decoration: BoxDecoration( border: Border. all(color: Colors. green[400], width: 3) ), child: Text("I am inside a border.", style: TextStyle(fontSize: 24),), );

How do you add a Gradient to an outline?

On the Format tab, click Shape Fill, click Gradient, and select the one you want.

How do you add Gradient color to text in flutter?

Using a shader for Gradient text We can create a custom shader and use that as the foreground option for our text element. It looks like this: final Shader linearGradient = LinearGradient( colors: <Color>[Colors. pink, Colors.


1 Answers

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

enter image description here

Storyboard:

enter image description here

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)
like image 181
AtulParmar Avatar answered Oct 10 '22 05:10

AtulParmar