如何在 Swift 中为我的标签添加轮廓?

How do I add an outline to my label in Swift?

我是 Whosebug 的新手。
我目前正在为 iOS.
使用 XCode 开发移动应用程序 但是,我正在尝试将白色 outline/stroke 添加到我的标签中,但我不知道该怎么做。我曾尝试搜索这些论坛,但在 swift.
中找不到任何解决方案 我试过使用影子属性,但效果不尽如人意
如何在 Swift 中为我的标签添加轮廓?
编辑:文字应类似于此图片中的文字: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

您需要使用NSAttributedString,设置strokeColorstrokeWidth设置轮廓,foregroundColor设置文本颜色。试试这个:

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

这将如下所示:

你可以使用这个OutlinedLabel。与大多数示例不同,它实际上概述了文本。 你甚至可以让轮廓渐变。

只需设置轮廓颜色和线宽:

label.outlineColor = .white
label.outlineWidth = 7

这看起来像 this