我正在构建一个输出到 PDF 文件中的大字符串,但是现在,我想在我的文档中有一个 2 列的项目符号列表。但是,我还没有弄清楚可以让我获得所需的标签效果的正确设置。
目前,我正在测试以下代码:
let mutableString = NSMutableAttributedString()
let words = ["this", "is", "really", "getting", "old"]
let paragraphStyle = NSMutableParagraphStyle()
var tabStops = [NSTextTab]()
let tabInterval: CGFloat = 250.0
for index in 0..<12 {
tabStops.append(NSTextTab(textAlignment: .left,
location: tabInterval * CGFloat(index),
options: [:]))
}
paragraphStyle.tabStops = tabStops
for index in 0..<words.count {
if index != 0 && index % 2 == 0 {
mutableString.append(NSAttributedString(string: "\n"))
}
if index % 2 == 1 {
let attributedText = NSAttributedString(string: "\t", attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
mutableString.append(attributedText)
}
let word = words[index]
let attributedString = NSMutableAttributedString(string: "\u{2022} \(word)",
attributes: [:])
mutableString.append(attributedString)
}
当我将它输入我的 PDF 生成器时,它会产生以下结果:
最终,我希望“is”和“getting”与文档的中间对齐,这样我就可以容纳更大的单词。