2

I am building a RSS app, and am working on displaying all of the content inside the app, not in a WebView. I am trying to figure out how to create custom styles for each HTML element in a UILabel.

HTML text in UILabel with Attributed String: enter image description here

I know that this can be done with a NSAttributedString. I've been able to display the HTML text in the UILabel (see screenshot) through an attributed string:

do {
    let attrStr = try NSAttributedString(
        data: (content.data(using: String.Encoding.unicode, allowLossyConversion: true)!),
        options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
        documentAttributes: nil)
    bodyLabel.attributedText = attrStr
} catch let error {
    print(error)
}

However I can not figure out how to set the text in between each tag to have the specific style associated with that tag. Here is an example of the HTML code I am trying to style:

<p>Back in 2006, when the iPhone was a mere rumor, Palm CEO Ed Colligan was asked if he
  was worried:</p>

  <blockquote>
    <p>&ldquo;We&rsquo;ve learned and struggled for a few years here figuring out how to
    make a decent phone,&rdquo; he said. &ldquo;PC guys are not going to just figure this
    out. They&rsquo;re not going to just walk in.&rdquo; What if Steve Jobs&rsquo;
    company did bring an iPod phone to market? Well, it would probably use WiFi
    technology and could be distributed through the Apple stores and not the carriers
    like Verizon or Cingular, Colligan theorized.</p>
  </blockquote>

  <p>I was reminded of this quote after Amazon <a href=
  "http://phx.corporate-ir.net/phoenix.zhtml?c=176060&amp;p=irol-newsArticle&amp;ID=2281414">
  announced an agreement to buy Whole Foods</a> for $13.7 billion; after all, it was only
  <a href=
  "https://www.bloomberg.com/news/articles/2015-01-29/in-shift-whole-foods-to-compete-with-price-cuts-loyalty-app">
  two years ago</a> that Whole Foods founder and CEO John Mackey predicted that groceries
  would be Amazon&rsquo;s Waterloo. And while Colligan&rsquo;s prediction was far worse
  &mdash; Apple simply left Palm in the dust, unable to compete &mdash; it is Mackey who
  has to call Amazon founder and CEO Jeff Bezos, the Napoleon of this little morality
  play, boss.</p>

EDIT: So I've made some progress. The original problem of isolating the inside of specific HTML elements for styling still persists, but I have gotten to the point where this is the only problem.

if let htmlText = content.htmlAttributedString {
        let attributes = [NSFontAttributeName: UIFont.systemFont(ofSize: 19.0)]
        let attributedText = NSMutableAttributedString(string: htmlText.string, attributes: attributes)

        let htmlString = htmlText.string as NSString
        let range  = htmlString.range(of: title)
        attributedText.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 24.0), range: range)

        bodyLabel.attributedText = attributedText
}

var htmlAttributedString: NSAttributedString? {

    do {
        let attrStr = try NSAttributedString(
            data: (self.data(using: String.Encoding.unicode, allowLossyConversion: true)!),
            options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
            documentAttributes: nil)
        return attrStr
    } catch let error {
        print(error)
    }
    return nil

}
Jeremy Spence
  • 63
  • 1
  • 5
  • 1
    https://stackoverflow.com/questions/28496093/making-text-bold-using-attributed-string-in-swift/28532609?s=2|0.0000#28532609 – Leo Dabus Aug 06 '17 at 07:56
  • 1
    Thanks Leo! The problem for me however is not actually styling these elements, but isolating the text that needs to be styled from the actual HTML – Jeremy Spence Aug 06 '17 at 08:01
  • You need separate your blockquotes? to handle them into UIWebView? is that what you need? – Reinier Melian Aug 06 '17 at 08:07
  • do you the style in advance or do you need to pull in that info dynamically with the text? – JAB Aug 06 '17 at 08:10
  • Hi Reinier and BJH - All of that HTML code is pulled in directly through the RSS protocol I'm using I did not style that at all. – Jeremy Spence Aug 06 '17 at 18:20
  • Could you show what is your wanted result, and explain which part you want to isolate and what effect you want to add to it? – Larme Aug 08 '17 at 09:04

0 Answers0