NSAttributedString项目符号列表问题

时间:2017-10-23 12:06:29

标签: ios swift uitextview nsattributedstring

我正在尝试使用NSAttributedStringUITextView创建项目符号列表。而且,这是迄今为止我能够实现的目标:enter image description here

可以看出,两条线之间存在一个小的“转变”。这是我用来构建属性字符串的代码片段:

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)\n"
        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

但现在我让所有的线条相互对齐,包括子弹点:

enter image description here

更新2

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

2 个答案:

答案 0 :(得分:5)

@ the4kman,@ Krunal,谢谢你的回复!解决方案更加简单。用\t中的let formattedString = "\u{2022} \(string)\n替换空格符号可以得到有效的缩进。

为了完整性,完整的解决方案代码是(只替换一个字符):

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)\t\(string)\n"
        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
}

答案 1 :(得分:-1)

这是一个简单的Objective-C代码段,主要基于公认的答案:

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    Toast.makeText(this.getContext(),  this.getArguments().getString("1"), Toast.LENGTH_SHORT).show();
    ....