NSAttributedString:非段落标题

时间:2015-02-22 17:02:28

标签: ios swift nsattributedstring textkit

我正在为每个单词构建一个包含多个UITextView的大NSAttributedString字典。我试图在下面的行之前设置一个固定的空格,但是我不能在“L'ensemble”开头的文本上设置标题,因为它不是段落(不以\ n开头)。

你有想法实现这个目标吗?

到目前为止,这是我的代码无效,因为valueParaStyle没有做任何事情,因为valueText不以\ n开始。

感谢。

let senseNumberParaStyle = NSMutableParagraphStyle()
senseNumberParaStyle.paragraphSpacingBefore = 20

let valueParaStyle = NSMutableParagraphStyle()
valueParaStyle.firstLineHeadIndent = 20
valueParaStyle.headIndent = 20

for i in 0..<senses.count {
    let senseNumberText = NSAttributedString(string: "\n\(i + 1).", attributes: [
        NSFontAttributeName: UIFont(name: "AvenirNext-Bold", size: 14)!,
        NSForegroundColorAttributeName: UIColor(red: 1, green: 0.275, blue: 0.294, alpha: 1),
        NSParagraphStyleAttributeName: senseNumberParaStyle
    ])

    wordText.appendAttributedString(senseNumberText)

    if let value = senses[i].value {
        let valueText = NSAttributedString(string: " \(value)", attributes: [
            NSFontAttributeName: UIFont(name: "AvenirNext-Regular", size: 15)!,
            NSForegroundColorAttributeName: UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1),
            NSParagraphStyleAttributeName: valueParaStyle
        ])

        wordText.appendAttributedString(valueText)
    }
}

1 个答案:

答案 0 :(得分:2)

你走在正确的轨道上。您只需在第一行添加"\t"

 var paragraphStyle = NSMutableParagraphStyle()
 paragraphStyle.headIndent = 20

 label.attributedText = NSAttributedString(string: "1.\tHere comes your text. Indent as you really want it", attributes:[NSParagraphStyleAttributeName : paragraphStyle])

没有"\t"

enter image description here

使用"\t"

enter image description here