Search code examples
iosswift3uilabelborder

Text Shadow - iOS, swift 3.0


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


Solution

  • 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