Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML with <blockquote> tags in UITextView

I want to display HTML text with <blockquote> tags in the UITextView.

example of HTML:

<div>
    <blockquote class="uncited">
        <div>
            <cite>Nick:</cite>
            <blockquote class="uncited">
                <div>
                    <cite>Tom:</cite>censored<br>
                    Hello
                </div>
             </blockquote>
             <p>World</p>
         </div>
    </blockquote>
    <p>Text</p>
</div>

I use the following code to display HTML text in the cell:

UITextView *textView = (UITextView*)[cell viewWithTag:100];

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[thisComment.htmlText dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
textView.attributedText = attributedString;

But in the UITextView it's looks like plain text. I want to get it: sample How can i do this? What kind of libraries should i use? i think about HTML -> Markdown -> NSAttributedString -> TextView

like image 232
Ji11erBug Avatar asked May 01 '14 14:05

Ji11erBug


1 Answers

Apple's HTML to AttributedText parser is excellent with CSS so you can actually write it like you'd do for a raw web client.

let parsedCommentHTML = html.replacingOccurrences(of: "<blockquote>\n", with: "<blockquote>\n<k style=\"color:#ccc; font-size: 2em; font-family: 'Copperplate'\">“</k>")
let blockQuoteCSS = "\nblockquote > p {color:#808080; display: inline;} \n blockquote { background: #f9f9f9;}"
let pCSS = "p {margin-bottom: 0px;}"
let cssStyle = "\(blockQuoteCSS)\n\(pCSS)\n"

return try NSAttributedString(data: ("<html><head><style>\(cssStyle)</style></head><span style=\"font-family: HelveticaNeue-Thin; font-size: 17\">\(CONTENT)</span></html>").data(using: String.Encoding.unicode)!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil)

This produces a nice (in my opinion) looking quote: enter image description here

like image 106
Allison Avatar answered Nov 17 '22 09:11

Allison