Ale*_*way 10 cocoa nsattributedstring ios ios7 textkit
随着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;
Run Code Online (Sandbox Code Playgroud)
您应该能够停止可变的 paragraphStyle 然后设置其缩进,例如:
NSMutableParagraphStyle *const bulletParagraphStyle = [paragraphStyle mutableCopy];
bulletParagraphStyle.firstLineHeadIndent = 20;
bulletParagraphStyle.tailIndent =20;
Run Code Online (Sandbox Code Playgroud)
然后仅在包含项目符号点的文本范围内设置此段落样式。
(尝试从 HTML 开始会有点笨拙,我想:如果你想保留该路线,可能会将其分成两个 HTML 字符串,一个是项目符号列表,另一个是标题,所以您可以更轻松地设置不同的属性。)
| 归档时间: |
|
| 查看次数: |
1829 次 |
| 最近记录: |