Text Shadow - iOS, swift 3.0

I am trying to make the shadow size a little larger, but I cannot do it.

:

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 the shadow to be larger than the text itself?

something like that.

example

Maybe you can do with the border, My text is the title UIButton!!! I expect it to be all uiButton text

+4
source share
2 answers

You can follow this path to reach the selected text.

You must use an attributed string and use the property setAttributedTitleto get the desired result.

Here is the code:

    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)

Conclusion: enter image description here

Happy coding ...

+1
source

You can do it this way

setTitleShadowColor titleLabel?.layer.shadowColor

    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)

,

:

enter image description here

+6

Source: https://habr.com/ru/post/1678153/


All Articles