5

I'm trying to make shadow size a bit bigger but I can't do it.

so far:

findAPlace.titleLabel?.layer.shadowOffset = CGSize(width: -1, height: 1)
findAPlace.titleLabel?.layer.shouldRasterize = true
findAPlace.titleLabel?.layer.shadowRadius = 1
findAPlace.titleLabel?.layer.shadowOpacity = 1
findAPlace.titleLabel?.layer.shadowColor = UIColor(red:0.07, green:0.07, blue:0.07, alpha:1.0).cgColor

how to scale shadow to be bigger than the text itself?

something like this.

example

Maybe with a border can be done, My text is the title of a UIButton!!!I expect it to be all around text of the uiButton

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
Dpetrov
  • 546
  • 2
  • 5
  • 12
  • This seems to ba aduplicate from this question: https://stackoverflow.com/questions/1103148/how-do-i-make-uilabel-display-outlined-text – pesch May 30 '17 at 09:20
  • i'm trying to add to button title, not to label, its different and i cant figure it out! – Dpetrov May 30 '17 at 09:27
  • You need to use `yourButton.setTitle("Your title", forState: .normal)` https://developer.apple.com/reference/uikit/uibutton/1624018-settitle – pesch May 30 '17 at 09:30
  • i know, i want to customize the title text!!! – Dpetrov May 30 '17 at 09:33
  • Looks like you want a font outline, not a shadow. So maybe this will help: http://mabdev.com/2016/06/09/how-to-outline-your-uilabel-uibutton/ – koen May 30 '17 at 09:38
  • @Dpetrov have you found a proper solution to outline the title of a UIButton? I could not find any workaround so far – Gasper J. Feb 15 '20 at 20:41
  • can't remember what i did, i already moved on. – Dpetrov Feb 17 '20 at 11:21

2 Answers2

6

You can do in this way

Actually You need to use setTitleShadowColor instead of titleLabel?.layer.shadowColor

Here is full working code

    let btnTemp = UIButton(type: .custom)
    btnTemp.frame = CGRect(x: 50, y: 200, width: 150, height: 40)
    btnTemp.setTitle("Hello", for: .normal)
    btnTemp.titleLabel?.layer.shouldRasterize = true
    btnTemp.titleLabel?.layer.shadowRadius = 1.0
    btnTemp.titleLabel?.layer.shadowOpacity = 1.0
    btnTemp.setTitleColor(UIColor.blue, for: .normal)
    btnTemp.backgroundColor = UIColor.gray
    btnTemp.titleLabel?.shadowOffset = CGSize(width: -1, height: 1)
    btnTemp.setTitleShadowColor(UIColor(red:0.07, green:0.07, blue:0.07, alpha:1.0), for: .normal)
    self.view.addSubview(btnTemp)

Hope it helps

Output:

enter image description here

Community
  • 1
  • 1
Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
1

You can follow this way to achieve the outlined text.

You have to use attributed string and use setAttributedTitle property of button to get required result.

Here is the code:

Swift 4

let strokeTextAttributes: [NSAttributedStringKey : Any] = [
    NSAttributedStringKey.strokeColor : UIColor.red,
    NSAttributedStringKey.foregroundColor : UIColor.gray,
    NSAttributedStringKey.strokeWidth : -2.0,
]

let attributedString = NSAttributedString(string: "text", attributes: strokeTextAttributes)
self.btnTemp.setAttributedTitle(attributedString, for: .normal)

Swift 3

let strokeTextAttributes = [
    NSStrokeColorAttributeName : UIColor.red,
    NSForegroundColorAttributeName : UIColor.gray,
    NSStrokeWidthAttributeName : -2.0,
] as [String : Any]

let attributedString = NSAttributedString(string: "text", attributes: strokeTextAttributes)
self.btnTemp.setAttributedTitle(attributedString, for: .normal)

Output:

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Janmenjaya
  • 4,149
  • 1
  • 23
  • 43