简体   繁体   English

如何将填充添加到UILabel,其文本包含NSBackgroundColorAttributeName

[英]how to add padding to a UILabel who's text includes a NSBackgroundColorAttributeName

I'm having trouble trying to figure out a way to add padding to my UILabel which includes an attributed string that contains a background color for text. 我在试图找到一种向我的UILabel添加填充的方法时遇到了麻烦,其中包含一个包含文本背景颜色的属性字符串。

as shown in the image, there isn't any left padding on the label's text - i need to be able to add some padding to the left and the right. 如图所示,标签的文本上没有任何左边的填充 - 我需要能够在左边和右边添加一些填充。 here is my current code: 这是我目前的代码:

var attributedString = NSMutableAttributedString(string: "\(blueHighlightedText)")
attributedString.addAttribute(NSBackgroundColorAttributeName, value: UIColor(red: 0.369, green: 0.718, blue: 0.882, alpha: 0.83), range: NSMakeRange(0, blueHighlightedText.bridgeToObjectiveC().length))
attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, blueHighlightedText.bridgeToObjectiveC().length))
attributedString.addAttribute(NSKernAttributeName, value: -0.35, range: NSMakeRange(0, blueHighlightedText.bridgeToObjectiveC().length))

attributedText = NSAttributedString(attributedString: attributedString)

How can i add padding to this? 我该如何为此添加填充?

设置标签的contentEdgeInsets,或将标签作为子视图添加到彩色视图和位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM