使用 NSHTMLTextDocumentType 的要点间距和换行符起始位置



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;

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

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

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

(尝试从HTML开始会有点笨拙,我想:如果你想保持这条路线,可以把它分成两个HTML字符串,一个是项目符号列表,一个是标题,这样你就可以更容易地设置不同的属性。

最新更新