10

I have looked around and my current situation uses MWFeedParser which is a fantastic parser. What my app does is read a news website feed, display the news stories in a table, then select which story you want and display the full story in a new ViewController.

So far I have this functionality down, but I'm struggling to work with the text. After using .dataUsingEncoding(NSUTF16StringEncoding) on the downloaded XML data, this turns the unreadable text in to clear readable text.

My problem arises when I have to implement the adding of the image, I have a way of getting the image and adding it to the story in my table view next to the title, but I do not know how to keep the struct of the text on my full story ViewController as is on the news website (so adding the image into the right part of the text: Example, after second paragraph)

If anyone can tell me how to add proper spacing where paragraphs should be that would be awesome. General manipulation of this text would be helpful I am completely clueless so far.

IKavanagh
  • 6,089
  • 11
  • 42
  • 47
Dom Bryan
  • 1,238
  • 2
  • 19
  • 39

1 Answers1

0

I would guess, and as per the example of text provided @pi1000 in comments, the bit of text that's giving you trouble is HTML?

If that is indeed the case, I would suggest using NSAttributedString in your textview, which I'm guessing is in your full story ViewController.

let encodedData = encodedString.dataUsingEncoding(NSUTF8StringEncoding)!
let attributedOptions : [String: AnyObject] = [
    NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
    NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding
]
let attributedString = NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil, error: nil)!

Also, if you're dealing with arbitrary internet content, I would urge you to use UIWebView or WKWebView instead of a textview in your full story ViewController, as those classes are much better at handling arbitrary HTML.

Sam J.
  • 685
  • 1
  • 8
  • 22
  • Yeah, I know that method, but I'm in need of including images and videos. – fredpi Nov 04 '15 at 07:32
  • @pi1000 For what you need, use a UIWebView or WKWebView, instead of a UITextView. Method to use is: `webView.loadHTMLString(text, baseURL: nil)`. – Sam J. Nov 04 '15 at 11:41