ios - 无法将 NSAttributedString 呈现为 PDF 中的 2 列选项卡式项目符号列表

标签 ios swift nsattributedstring nstexttab nsmutableparagraphstyle

我正在构建一个输出到 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 生成器时,它会生成以下结果:

enter image description here

最终,我希望“is”和“getting”与文档的中间对齐,以便可以容纳更大的单词。

最佳答案

事实证明我在球场内,但绝对不是接近。

以下提供了所需的分栏效果:

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.tabStops = [
    // 274 would be the midpoint of my document
    NSTextTab(textAlignment: .left, location: 274, options: [:])
]

let string = "\u{2022} This\t\u{2022} is\n\u{2022} getting\t\u{2022} really\n\u{2022} old"

let attributedString = NSAttributedString(
    string: string,
    attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle]
)

enter image description here

为了奖励积分,如果您想在文档中包含多个列,则以下内容将实现此目的(请原谅我的粗略格式):

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.tabStops = [
    NSTextTab(textAlignment: .left, location: 100, options: [:]),
    NSTextTab(textAlignment: .left, location: 300, options: [:])
]

let string = "\u{2022} This\t\u{2022} is\t\u{2022} getting\n\u{2022} really\t\u{2022} old"

let attributedString = NSAttributedString(
    string: string,
    attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle]
)

看起来像这样:

enter image description here

这是怎么回事?

所以,我在这里学到的是 tabStops 告诉 iOS 行中放置选项卡的位置:

  1. 第一个选项卡将转到位置 100
  2. 第二个选项卡将转到位置 300
  3. 第三个选项卡将环绕文档并转到位置 100

关于制表符,如果您在第一个索引中分配位置为 0 的制表符,则按制表键到换行符将使其与左边缘对齐。

什么为我解决了这个问题。我所依赖的方法是在遇到字符串的每个组件时添加它。但是,该字符串将无法正确格式化。相反,通过将所有内容合并到一个字符串中并应用我的工作代码中看到的属性,我能够使其正确对齐。

我还使用我的问题中看到的各个组件进行了测试,但也应用了段落样式属性,这也产生了一个可行的解决方案。

基于此,我的错误似乎是混合了具有和不具有所需的制表行为的字符串。

关于ios - 无法将 NSAttributedString 呈现为 PDF 中的 2 列选项卡式项目符号列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56483012/

相关文章:

ios - SceneKit 场景大小不正确

swift - 类实例变量初始化顺序?

iOS NSAttributedString 到 HTML

ios - 根据其原始纯文本状态创建 UIButton 属性标题

objective-c - 如何从 Modal ViewController 重新加载父 UITableViewController 中的数据

iphone - Xcode:此方法之前缺少正确的分隔符

swift - MKMapView NSInvalidArgumentException 无效区域在 ios 9 swift 中崩溃?

ios - NSAttributedString EXC_BAD_ACCESS KERN_INVALID_ADDRESS 崩溃

iphone - 逐行 2 列的表格 View ,iphone/ipad

ios - NSDate 不是 Objective-C 对象?