我想使用NSAttributedString
为文本设置样式。文本应具有背景和自定义填充,以使文本与背景边缘之间有少许空间。
这是我想要实现的:
这是我不想实现的(第二行的背景不是单词/字符专用的:: >>
这是我在操场上尝试过的代码:
let quote = "some text with a lot of other text and \nsome other text." let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .left let attributes: [NSAttributedString.Key: Any] = [ NSAttributedString.Key.paragraphStyle: paragraphStyle, NSAttributedString.Key.backgroundColor: UIColor.red, NSAttributedString.Key.foregroundColor: UIColor.white ] let attributedQuote = NSAttributedString(string: quote, attributes: attributes)
这是游乐场预览呈现的内容:
[![操场预览] [3]] [3]
文本非常靠近背景边缘。有什么方法可以使文本的背景具有一定的空间?我需要一些填充物。我尝试使用headIndent
,但是那样会将带有背景的文本移到右侧,而不仅仅是文本。因此,它对于填充没有用。
我想使用NSAttributedString设置文本样式。文本应具有背景和自定义填充,以使文本与背景边缘之间有一些空间。这就是我想要的...
文本应具有背景和自定义填充,以便文本与背景边缘之间有一点空间。