11

随着 iOS 7 的到来NSHTMLTextDocumentType,我使用下面的代码来解析 html 并将其显示在UITextView. 它完美地工作,除了要点。

如何更改项目符号每一侧的间距(项目符号与UItextView左边框之间的空间以及项目符号与其右侧文本之间的空间)?

还有,更重要的是。如果文本在下一行继续,我还需要它在相同的 x 位置继续,就像它上面的项目符号文本开始的行一样。我怎样才能做到这一点?(第二行缩进)

我尝试过使用各种 css,但似乎NSHTMLTextDocumentType仍然相当有限。我设法改变的只是列表的颜色。

感谢所有帮助。

先感谢您!

代码:

_textView.textContainer.lineBreakMode = NSLineBreakByCharWrapping;

NSString *testText = @"<p><b>This is a test with:</b></p><ul><li>test test test
test test test test test test test test test test <li>test test test test test
test <li>test test test test test <li>test test test test test test test test
test test test test <li>test test test test test test test test <li>test test
test test test test <li>test test test test test test test test test
</li></ul>";

    NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
    NSData *htmlData = [testText dataUsingEncoding:NSUnicodeStringEncoding];

    _attributedString = [[NSMutableAttributedString alloc] initWithData:htmlData
    options:options documentAttributes:nil error:nil];

    // Paragraph style
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.paragraphSpacing = 0;
    paragraphStyle.lineHeightMultiple = 1.0f;
    paragraphStyle.maximumLineHeight = 30.0f;
    paragraphStyle.minimumLineHeight = 20.0f;

    // Adding attributes to attributedString
    [_attributedString addAttributes:@{NSParagraphStyleAttributeName:paragraphStyle}
                              range:NSMakeRange(0,_attributedString.length)];
    [_attributedString addAttributes:@{NSFontAttributeName:font}
               range:NSMakeRange(0,_attributedString.length)];

    // Setting the attributed text
    _textView.attributedText = _attributedString;
4

1 回答 1

2

您应该能够停止可变的段落样式,然后设置其缩进,例如:

NSMutableParagraphStyle *const bulletParagraphStyle = [paragraphStyle mutableCopy];
bulletParagraphStyle.firstLineHeadIndent = 20;
bulletParagraphStyle.tailIndent =20;

然后仅在包含项目符号的文本范围内设置此段落样式。

(尝试从 HTML 开始会有点笨拙,我认为:如果您想继续使用该路线,可以将其分成两个 HTML 字符串,一个是项目符号列表,一个是标题,所以您可以更轻松地设置不同的属性。)

于 2014-01-07T11:09:00.800 回答