iOS - Converting HTML to Normal text - html

In my application, I'm receiving an html file from a news server.
After receiving, I want to remove the tags, images, URL anchors, etc and just show the text in text view.
There's a website which functions similar to the one that I'm looking for. The website takes html as input and removes the tags and displays just plain text as result. I want to achieve similar function in my app and display only the text from the news received.
Any libraries or open source web services available for this?

There is this library here, you could do something like this:
NSString *htmlStripped = [[NSAttributedString attributedStringWithHTML:DATA_FOR_MY_HTML options:nil] string];

Related

HTML table on a UIView in Swift

I am trying to display a table in a UIView that is delivered in html form from a server. It looks like these tables might be slightly different, but they obviously display correctly in a browser.
Is there anyone that can recommend a library that is useful for this?
Thanks for any help.
Edit screenshot of the table cell
The 'textBlock' gets built up with text, images and hopefully tables. All by appending the attributed text. I can't see a way to switch that label out for a webView.
If you want to display HTML in iOS it's better to use WKWebView instead of UIView. Then you can use loadHTMLString method:
webView.loadHTMLString("<html><body><p>Hello!</p></body></html>", baseURL: nil)

Remove html/text from RSS feed description except images

I am setting up a blog with WordPress, that uses a plugin to import RSS feeds and automatically publish them to the blog, on a schedule.
I only want to pull the images from the descriptions, not the text that sometimes appears with them, or other html elements.
There could be multiple images in a post, each one with captions, or links.
Ideally I'd like to use Yahoo Pipes to grab the feed, then use regex operator to replace everything with blank except <img> elements. Then send the manipulated feed to the WP plugin.
I've only managed to strip paragraphs so far, using: <p>.*?</p>. But in some cases there is plain text not wrapped in tags, etc.
Any help appreciated :) I'm a bit of a regex newbie.
you can try with this to get all images from html code.
preg_match_all('/<img[^>]+>/i',$html, $allimages);
print_r($allimages);
if you want image to be stored in string format then implode it with ,

Text heavy iOS App. Store text in HTML, Plist, or Other?

I'm writing relatively complex iOS app that is very text heavy.
The text is also heavily formatted. It has lots of color, size, font, and spacing changes, as well bulleted lists and other text features you'd expect to see in a very rich website.
The text is displayed on about 40 different views. Some of which display a lot of text, others a little. There is no one template that all the pages follow. (There are some that are similar, but that's not the point.)
Lastly, the text is constantly being changed and updated by an editorial team during development, not so much after release. The text has to be stored on the device, downloading files is not an option.
My question is, what is the best way to store and then render all this text in an iOS App?
My approach
Store all the text content and formatting info in an html file and use
[[NSAttributedString alloc] initWithFileURL:htmlDoc
options:#{
NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
documentAttributes:&attrDict
error:&error];
to create a NSAttributed string and use that to populate UITextViews.*
*Note: I would do some more work before creating the UITextViews. First I would parse it to find the appropriate page number [[Page:1.3]] and then parse the elements in that section [[header]], [[side_scroller]], etc...
I like this approach for two main reasons:
It created a separate copy document that contained all the text
and formatting info.
I'm the only iOS developer, but we have a couple front-end
developers. So when we get slammed with changes that need to be done
in 3.45 minutes, I could have some of the guys help me make the
changes, without having to know all the nuances of UIFont and
related classes. Occasionally, the editors could even make the
changes themselves :)
Minor reasons for liking this approach:
The text can vary so much per page, that creating a new UIFont + Plist entry to store the formatting info seems like a bigger pain than having everything in a .html document. (I could be wrong about this.)
Project managers will inevitably say: "Make this word a little bigger," "This word looks strange, add italics," and "Make everything purple!" HTML/CSS seems like a more flexible solution for quickly implementing these requests.
Downsides of this approach:
NSAttributedString picks up 99% of the HTML attributes I threw at it. It did not pick bullet spacing changes in unordered lists <ul>.
Plists are more performant.
Here are some other approaches I considered:
Plist + UIFont
RTF Document - Originally started with this, but found it hid a lot of what was going on and NSAttributedString wouldn't pick up some of the changes.
XML
Any advice or input would very appreciated.
Notes:
iPad app,
iOS 7,
No Internet Connectivity,
Xcode 5
What I did to store styled text in an iOS app was to write a Mac OS command line tool that opens RTF files and converts them to attributed strings (It's a 1-line call in Mac OS, but not supported in iOS for some reason.) I then use NSCoding to save the attributed strings as binary data, with a special .DATA filetype.
I created a custom UITextView category with a method that knows how to load the text view's attributed text from my custom filetype.
I created a build rule in my project that treats RTF files as source files in a build step and the .DATA filetype as the output, and copies the .DATA files into the build project.
Now, all I have to do is add an RTF file to my project the build process inserts the .DATA version of the styled text into the executable.
The Xcode editor knows how to edit RTF files, so you can edit them right in place in the IDE, OR you can edit them in TextEdit or any editor that supports RTF files.
There are a few things you can put in an RTF that aren't supported in UITextViews. (I don't remember what those are offhand. Sorry.)
I find styled WYSIWYG text much easier to deal with than HTML. You just edit the text, and the build process picks up the changes.
It worked beautifully. Plus, binary NSCoding output is a whole lot more compact than HTML.
I would recommend using web view. It can open files in resource bundle.
You can disable all the links in HTML by implementing delegate method shouldStartLoadWithRequest to return NO.
You might also want to set dataDetectorTypes to UIDataDetectorTypeNone.
That will disable auto link detection in web view

How do you display text using formatting in the form of a MVC3 view?

I have a MVC3 web project that I am trying to display text from an xml doc in the form area of one of my views.
I would like the xml to be displayed with color coding and pretty print indenting like the xml text that is displayed when you open an xml file using IE.
Currenty I am using the #Html.Display("xmlfiletext").
The text is displayed as mono color with no new lines or carriage returns.
There are lots of syntax highlighting plugins that allow you to achieve that. Google prettify is just one example.

iOS Printing and HTML formatter - Which interpreter and how fancy?

I have an iOS 5 application that sends a job to the print que. I have all of that code working fine and I'm using the HTML formatter to create the document (UIMarkupTextPrintFormatter). Right now I have a very basic HTML string that I am using for testing but want to flesh it out further.
Which HTML engine is used to interpret the HTML string that is passed to UIMarkupTextPrintFormatter? Or, in other words, what is the best way for me to test my HTML string outside of XCode? Can I just create a text file and then view it in Safari? Would that give me the best approximation of what it will look like when printed from iOS?
How fancy can I get with the HTML in the UIMarkupTextPrintFormatter? I assume that I'll have to use all inline styles? Are there some examples out there of HTML strings that are sent to UIMarkupTextPrintFormatter to create rich documents? I just want to get an idea of what's possible with this combination.
You can test iOS printing in the Simulator, using the iOS Printer Simulator app. The UIMarkupTextPrintFormatter will render through WebKit. Imagine the HTML string is a single-page HTML document. That is basically the limitations of the formatter, although I believe you can also link to external style sheets and images too.