I'm new to Stackoverflow.
I am currently developing a mobile application using XCode for iOS.
However I'm trying to set add a white outline/stroke to my label but I do not know hot to. I have tried searching these forums but could not find any solution in swift.
I have tried using the shadow property, but it's not satisfactory.
How do I add an outline to my label in Swift?
Edit: The text should look like the text in this picture: http://cf.chucklesnetwork.com/items/7/5/7/4/4/original/yo-dawg-i-heard-you-like-captions-so-i-put-captions-of-captions.jpg
            Asked
            
        
        
            Active
            
        
            Viewed 6,920 times
        
    3
            
            
        
        Karliene
        
- 147
 - 1
 - 3
 - 10
 
- 
                    u can set border for the label – Catherine May 03 '18 at 11:31
 - 
                    please post image of your requirement. layer.border will set border – Vinodh May 03 '18 at 11:32
 - 
                    Welcome! Can you please share a bit of your current code to walk over. Like the label you have mentioned etc. – vahdet May 03 '18 at 11:32
 - 
                    2Possible duplicate of [Outline UILabel text in UILabel Subclass](https://stackoverflow.com/questions/40575408/outline-uilabel-text-in-uilabel-subclass) – Tamás Sengel May 03 '18 at 11:32
 - 
                    I would like my text to look like the text in this picture: http://cf.chucklesnetwork.com/items/7/5/7/4/4/original/yo-dawg-i-heard-you-like-captions-so-i-put-captions-of-captions.jpg – Karliene May 03 '18 at 11:34
 
2 Answers
18
            You need to use NSAttributedString, set strokeColor and strokeWidth  to set outline and foregroundColor to set text color. Try this:
let attrString = NSAttributedString(
    string: "Write Something with Outline",
    attributes: [
        NSAttributedStringKey.strokeColor: UIColor.black, 
        NSAttributedStringKey.foregroundColor: UIColor.white, 
        NSAttributedStringKey.strokeWidth: -2.0, 
        NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17.0)
    ]
)
yourLabel.attributedText = attrString
This will look like below:
2
            
            
        You can use this OutlinedLabel. Unlike most examples, it actually outlines the text. And you can even gradient the outline.
Just set the outline color and the line width:
label.outlineColor = .white
label.outlineWidth = 7
This will look like this
        Adriano
        
- 21
 - 1
 
