如何使用 NSParagraphStyle 在 UITextView 中创建项目符号列表?

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

我正在尝试在我的应用程序的

UITextView
中支持项目符号列表格式。我四处搜索并找到了建议使用NSParagraphStyle的问题的各种答案(如
here
)。现在要使给定的行成为项目符号格式,我在前面加上一个项目符号 (Unicode 2022) 加上一个制表符 (
•\t
) 并将段落样式应用于这样生成的行,给定缩进级别 >= 1 :

func makeBulletStyle(forLevel indentLevel: Int) -> NSParagraphStyle {
    let indentInterval = 15;
    let result = NSMutableParagraphStyle()
    
    //Indent the first line of this paragraph for its indent level
    result.firstLineHeadIndent = CGFloat(indentLevel * indentInterval)
    
    let tab = NSTextTab(textAlignment: .left, location: CGFloat(indentInterval))
    result.tabStops = [tab]
    
    result.defaultTabInterval = CGFloat(indentInterval)
    
    //Indent wrapped lines in this paragraph so they line up under the first line,
    //which is indented and then tabbed. Basically firstLineHeadIndent + indentInterval
    result.headIndent = CGFloat((indentLevel + 1) * indentInterval)
    
    return result
}

appears 如果我总是为行前缀使用相同的项目符号字符,则可以工作。但是,如果我切换到对不同级别使用不同的字符,就像大多数支持格式的文本编辑器一样 - 空圆圈三角形 等 - 那么缩进和制表符的距离就会变得不稳定,并且事情不再排列了。

这两个截图只改变了制表符:

我在这里做错了什么?为什么不同的制表符会导致问题?现在我在公认的不稳定假设下工作:

我也不太清楚缩进间隔在这里扮演什么角色,但如果我不设置它,那么每个列表项上的文本最终都会在项目符号本身后行上

ios uitextview nsattributedstring nsparagraphstyle
© www.soinside.com 2019 - 2024. All rights reserved.