NSAttributedString项目符号列表问题

NSAttributedString bullet list issues(NSAttributedString项目符号列表问题)

本文介绍了NSAttributedString项目符号列表问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NSAttributedStringUITextView创建项目符号列表。以下是我到目前为止所能做到的:

可以看到,两行之间有一个小的"移位"。下面是我用来构建属性字符串的一段代码:

func add(bulletList strings: [String],
         indentation: CGFloat = 15,
         lineSpacing: CGFloat = 3,
         paragraphSpacing: CGFloat = 10) {

    func createParagraphAttirbute() -> NSParagraphStyle {
        var paragraphStyle: NSMutableParagraphStyle
        let nonOptions = NSDictionary() as! [NSTextTab.OptionKey: Any]

        paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
        paragraphStyle.tabStops = [
            NSTextTab(textAlignment: .left, location: indentation, options: nonOptions)]
        paragraphStyle.defaultTabInterval = indentation
        paragraphStyle.firstLineHeadIndent = 0
        paragraphStyle.lineSpacing = lineSpacing
        paragraphStyle.paragraphSpacing = paragraphSpacing
        paragraphStyle.headIndent = indentation
        return paragraphStyle
    }

    var buffer = NSMutableAttributedString.init()

    for string in strings {
        let formattedString = "u{2022} (string)
"
        let attributedString = NSMutableAttributedString(string: formattedString)
        let paragraphStyle = createParagraphAttirbute()

        attributedString.addAttributes(
            [NSAttributedStringKey.paragraphStyle : paragraphStyle],
            range: NSMakeRange(0, attributedString.length))

        attributedString.addAttributes(
            textAttributes,
            range: NSMakeRange(0, attributedString.length))

        let string:NSString = NSString(string: formattedString)
        let rangeForBullet:NSRange = string.range(of: bulletPoint)
        attributedString.addAttributes(bulletAttirbutes, range: rangeForBullet)
        buffer.append(attributedString)
    }
}

您认为所选段落参数是否有问题?因为代码执行的几乎是预期的操作(不包括此差距)。

更新%1

遵循@the4kman建议,我更改了提供的代码,如下所示:

paragraphStyle.firstLineHeadIndent = indentation

但现在我将所有行彼此对齐,包括项目符号:

更新2

好的,解决方案非常简单-用制表符替换空格。请参阅下面更新的代码。

推荐答案

@the4kman,@krunal,感谢您的回复!解决方案就更简单了。将let formattedString = "u{2022} (string) 中的空格符号替换为 可提供有效的缩进。

为完整起见,完整的解决方案代码为(只需替换一个字符):

func add(bulletList strings: [String],
         font: UIFont,
         indentation: CGFloat = 15,
         lineSpacing: CGFloat = 3,
         paragraphSpacing: CGFloat = 10,
         textColor: UIColor = .black,
         bulletColor: UIColor = .red) -> NSAttributedString {

    func createParagraphAttirbute() -> NSParagraphStyle {
        var paragraphStyle: NSMutableParagraphStyle
        let nonOptions = NSDictionary() as! [NSTextTab.OptionKey: Any]

        paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
        paragraphStyle.tabStops = [
            NSTextTab(textAlignment: .left, location: indentation, options: nonOptions)]
        paragraphStyle.defaultTabInterval = indentation
        paragraphStyle.firstLineHeadIndent = 0
        paragraphStyle.lineSpacing = lineSpacing
        paragraphStyle.paragraphSpacing = paragraphSpacing
        paragraphStyle.headIndent = indentation
        return paragraphStyle
    }

    let bulletPoint = "u{2022}"
    let textAttributes: [NSAttributedStringKey: Any] = [.font: font, .foregroundColor: textColor]
    let bulletAttributes: [NSAttributedStringKey: Any] = [.font: font, .foregroundColor: bulletColor]
    let buffer = NSMutableAttributedString.init()

    for string in strings {
        let formattedString = "(bulletPoint)	(string)
"
        let attributedString = NSMutableAttributedString(string: formattedString)
        let paragraphStyle = createParagraphAttirbute()

        attributedString.addAttributes(
            [NSAttributedStringKey.paragraphStyle : paragraphStyle],
            range: NSMakeRange(0, attributedString.length))

        attributedString.addAttributes(
            textAttributes,
            range: NSMakeRange(0, attributedString.length))

        let string:NSString = NSString(string: formattedString)
        let rangeForBullet:NSRange = string.range(of: bulletPoint)
        attributedString.addAttributes(bulletAttributes, range: rangeForBullet)
        buffer.append(attributedString)
    }

    return buffer
}

这篇关于NSAttributedString项目符号列表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:NSAttributedString项目符号列表问题

基础教程推荐