我该如何在Swift中给我的标签添加轮廓线?

3
我是Stackoverflow的新手。我正在使用XCode为iOS开发移动应用程序。但是,我想在我的标签中添加一个白色轮廓/描边,但我不知道如何做。我已经尝试在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

你可以为标签设置边框。 - Catherine
图层边框将设置边框。 - Vinodh
欢迎!您能否分享一下您当前的代码,以便我们可以一起查看。比如您提到的标签等内容。 - vahdet
2
可能是 Outline UILabel text in UILabel Subclass 的重复问题。 - Tamás Sengel
我想让我的文本看起来像这张图片中的文本: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
2个回答

18

您需要使用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

这将会看起来像下面这样:

在此输入图片描述


请告诉我您在哪里遇到了这个错误? - Arnab
谢谢,问题已解决。 - Karliene
很高兴能够帮助 :) - Arnab

2
您可以使用这个OutlinedLabel。与大多数示例不同的是,它实际上勾勒出了文本。 而且,您甚至可以渐变描边。
只需设置描边颜色和线宽即可:
label.outlineColor = .white
label.outlineWidth = 7

这将会像这样显示。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接