简体   繁体   中英

NSAttributedString not showing paragraph indentation

I create an NSAttributedString from HTML like so.

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

When I display the string in a UITextView, everything is displayed well, except for paragraph indentations.

The HTML that is being converted to the NSAttributedString is.

"<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>"

Doing some research I learnt that </p> codes for space margins before and after paragraphs. Im guessing that the AttributedString is not "understanding" this or something.

Should NSAttributedString understand </p> ?

Why is this happening ?

How can I fix it?

Thanks!

<p> tags simply define paragraphs of text. They do not format a paragraph in the sense of indenting the first line if that is what you are referring to.

Here is an example of what your HTML will look like, notice the paragraphs do not indent.

Example:

https://jsfiddle.net/yoegpuxe/

Try entering a \\t before each paragraph if that is what you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM