NSAttributedString,在文本和背景前后带有填充符

问题描述 投票:0回答:1

我想使用NSAttributedString为文本设置样式。文本应具有背景和自定义填充,以使文本与背景边缘之间有少许空间。

这是我想要实现的:

what I want to achieve

这是我不想实现的(第二行的背景不是单词/字符专用的:: >>

what I don't want to achieve

这是我在操场上尝试过的代码:

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设置文本样式。文本应具有背景和自定义填充,以使文本与背景边缘之间有一些空间。这就是我想要的...

ios swift uilabel padding nsattributedstring
1个回答
0
投票

文本应具有背景和自定义填充,以便文本与背景边缘之间有一点空间。

© www.soinside.com 2019 - 2024. All rights reserved.