5

I've got some simple HTML with a blockquote tag I would like to show in a UILabel (which is inside UITableViewCell, so I don't think using UIWebView is the solution to my problem).

I decided to convert it to NSAttributedString and all worked fine, but what I received is quoted text looking exactly the same as text not being cited (no indentation, no block mark).

I would like to ask if there is any way you can achieve something similar to modern quote mentioned here? Or at least some indentation and prefix before every line of paragraph with quotation (ie > like oldschool email quotation) using NSAttributedString?

bjb568
  • 11,089
  • 11
  • 50
  • 71
co-
  • 331
  • 3
  • 7
  • Seems not: http://stackoverflow.com/questions/23409819/html-with-blockquote-tags-in-textview and this GitHub seems to do it: https://github.com/johnezang/NSAttributedString-Additions-for-HTML/blob/master/Classes/NSAttributedString%2BHTML.m – Larme Sep 07 '14 at 18:40
  • Of course I've seen mentioned stack topic, but i hoped that maybe something has changed since it was posted and somebody will answer my question. I'll test second link, but if its same as one used in DTCoreText than it will not work. – co- Sep 07 '14 at 20:26
  • i'm having same problem. How did you fixed it ? – aqsa arshad Mar 06 '17 at 05:01
  • Hi @aqsaarshad Well to be honest i produced some code which just searches over the whole string for opening and closing `blockquote` markers (using normal text-search). Than it removes nested blockquotes at some nesting level. After that it produces NSAttributedString with different Attributes for normal text and one in quotation. I would share the code with you, but as it is 3 years old, uncommented, not efficient and really dirty I think it's still better to write your own than to copypaste my solution. – co- Mar 07 '17 at 07:37
  • @co- ,Thanks for your help. I've also added a patch solution for now. I've added a style tag over HTML which will add "spacing , margins and background color" for all "blockquote" tags in HTML string and as a result i will get resultant attributed string with all styles mentioned in style tag. – aqsa arshad Mar 07 '17 at 07:46

1 Answers1

0

I understand that you have asked this in Obj-C, but I was just clicking through and noticed it's been three years and no answers and I'd done this already elsewhere so I might as well share.

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\">\(parsedCommentHTML)</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

Allison
  • 2,213
  • 4
  • 32
  • 56
  • I really want to use this code as a guide, but I have never coded in swift, so I can't convert it to objective-c, it would be very helpful if someone would. – jane Dec 06 '17 at 06:26
  • Well the important part really is the CSS. Read it like pseudo code. The only syntax you'll need to know is that in swift you can do `"string string \(variable)"` to insert `variable into the string. – Allison Dec 06 '17 at 06:27
  • I could read the let parts, but this return try NSAttributedString(data: ("\(parsedCommentHTML)").data(using: String.Encoding.unicode)!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil) is where I get confused. – jane Dec 06 '17 at 06:29
  • If you know what you're looking for (you probably don't, that's fair) it's easy to find: https://stackoverflow.com/a/18886718/1166266 – Allison Dec 06 '17 at 06:30