NSAttributedString未显示段落缩进

时间:2015-04-08 20:49:36

标签: ios uitextview nsattributedstring

我是这样从HTML创建一个NSAttributedString。

decodedString = [[NSAttributedString alloc] initWithData:[encodedString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil];

当我在UITextView中显示字符串时,除了段落缩进外,一切都会很好地显示。

正在转换为NSAttributedString的HTML是。

"<div><div class=\"article-content\">\n\t\t\t\t\t<p class=\"p1\"><a href=\"http://9to5mac.com/2015/04/08/apple-watch-ship-times-4-to-6-weeks-may-for-edition/apple_watch_liefer1/\" rel=\"attachment wp-att-373515\"><img title=\"Apple online store leaks Watch ship times: 4-6 weeks for some Watch models, &#x2018;May&#x2019; for Edition\" class=\"alignnone size-full wp-image-373515\" src=\"https://9to5mac.files.wordpress.com/2015/04/apple_watch_liefer1.jpg?w=704\" alt=\"apple_watch_liefer1\" width=\"320.000000\"></a></p>\n<p class=\"p1\"><span class=\"s1\">Apple Watch goes up for preorder on April 10 and becomes available April 24 through retail stores and online, but some of Apple&#x2019;s online stores are listing ship dates ranging from April 24th to sometime in May depending on the model. </span></p>\n<p class=\"p1\"><span class=\"s1\">Earlier today some of Apple&#x2019;s online stores, including its German and UK stores, listed various shipping dates for the device for those willing to do a little digging on the site. While the Sport models and Apple Watch with classic leather strap and Milanese bracelet are listed as shipping between April 24th and May 8th, other models are listed as shipping in 4-6 weeks or simply in &#x201C;May&#x201D;. </span></p>\n<p class=\"p1\"><span class=\"s2\"><a href=\"http://macerkopf.de\">macerkopf.de</a></span><span class=\"s1\"> first spotted the shipping dates on the German Apple online store and we later spotted the same dates on the UK and other international stores:</span>\n\t\t\t</p>\n\t\t\n\n<p>Apple Watch Sport: Usually ships &#x201C;24.04. to 5.8. &#x201D;<br>\nApple Watch with modern leather bracelet: Usually ships in &#x201C;4 to 6 weeks&#x201D;<br>\nApple Watch Link Watch: Usually ships in &#x201C;4 to 6 weeks&#x201D;<br>\nApple Watch with leather strap with loop: Usually ships in &#x201C;4 to 6 weeks&#x201D;<br>\nApple Watch with classic leather strap: Usually ships &#x201C;24.04. to 5.8. &#x201D;<br>\nApple Watch with Milanese bracelet: Usually ships &#x201C;24.04. to 5.8. &#x201D;<br>\nApple Watch with Sport Armband: Usually ships</p>\n<p class=\"p1\"><span class=\"s1\">It&#x2019;s unclear if these dates will apply to the US or just international markets, but it&#x2019;s not surprising considering we&#x2019;ve heard Apple Watch stock could be limited at launch. In addition to limiting stock of the gold Watch Edition and the <a href=\"http://9to5mac.com/2015/03/30/apple-watch-faq-preorders-try-on-appointments/\" target=\"_blank\">number of units customers can buy from each collection</a>, we reported earlier this month that <a href=\"http://9to5mac.com/2015/04/03/applecare-for-apple-watch-999-for-edition-79-for-watch-59-for-sport/\"><span class=\"s2\">supplies of the rose gold Edition models will be very tight</span></a> as Apple continues to work out discoloration issues.</span></p>\n<p class=\"p1\">You can get all the details on how preordering and purchasing the Apple Watch will work <a href=\"http://9to5mac.com/2015/03/30/apple-watch-faq-preorders-try-on-appointments/\" target=\"_blank\">in our FAQ here</a>.</p>\n<div id=\"jp-post-flair\" class=\"sharedaddy sd-like-enabled\"><div class=\"sharedaddy sd-block sd-like jetpack-likes-widget-wrapper jetpack-likes-widget-unloaded\" id=\"like-post-wrapper-22754319-373514-55255999c6ac5\"><h3 class=\"sd-title\">Like this:</h3><p class=\"likes-widget-placeholder post-likes-widget-placeholder\"><span class=\"button\"><span>Like</span></span> <span class=\"loading\">Loading...</span></p><a class=\"sd-link-color\"></a></div></div>\n\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\n\n\t\n\n\t\n\n\t\n\n\t\t\t\t</div>\n\t\t\t</div>"

进行一些研究我了解到</p>代码段落之前和之后的空间边距。我猜测AttributedString不是“理解”这个或类似的东西。

NSAttributedString应该理解</p>吗?

为什么会这样?

我该如何解决?

谢谢!

1 个答案:

答案 0 :(得分:1)

<p>标记只是定义文本段落。如果你指的是第一行,它们就不会格式化段落。

以下是HTML的外观示例,请注意段落不缩进。

示例:

https://jsfiddle.net/yoegpuxe/

如果您正在寻找,请尝试在每个段落前输入\t