Takasur
Takasur

Reputation: 462

How to add shadow to just UIButton border and not to text

Right now I am using this piece of code:

    donorButton.layer.shadowColor = UIColor.red
    donorButton.layer.shadowOffset = CGSize(width: 0, height: 2)
    donorButton.layer.shadowRadius = 2
    donorButton.layer.shadowOpacity = 1.0

And following is the output, whereas I want to get rid of the shadow on the text UIButton with shadow on border and text

Upvotes: 1

Views: 799

Answers (1)

trungduc
trungduc

Reputation: 12144

In my opinion, there are 2 solutions for your question.

  1. If you don't care about button background color, just need to change background color of button to non-transparent color.

    donorButton.backgroundColor = UIColor.white
    
  2. If you need a transparent background.

    • Give your button an empty text.
    • Put an UILabel below your button and give it same constraints with your button.
    • Set label.textAlignment = NSTextAlignment.center

Result

enter image description here

Upvotes: 3

Related Questions