is there anyway to display HTML content in textview? - iphone

i tried the following one from iphone cook book .i displayed HTML content through accessing private API of UITextview like
#interface UITextView (extended)
- (void)setContentToHTMLString:(NSString *) contentText;
#end
it works FINE.but Apple wont allow accessing private API.any solution pls?

Do you want to display actual HTML elements or just a string you took off the web? For the latter, you would do the following:
NSURL *stringURL = [NSURL URLWithString:#"http://yoursite.com/page.html"];
NSString *responseString = [NSString stringWithContentsOfURL:stringURL encoding: NSUTF8StringEncoding error:nil];
myTextView.text = responseString;

You could try using the Three20 library, which includes a styled text label:
http://github.com/facebook/three20/
The class TTStyledTextLabel of the Three20 framework has a text property "text" of type TTStyledText. This TTStyledText class has a textFromXHTML: static method which takes an NSString with any kind of HTML text inside, and might help you do what you want. However, it does not allow editing of the text, at least not as far as I know.
Hope this helps!

If you want HTML, your best bet is to use a dedicated UIWebView.

You can try this one.This is uses core text framework
https://github.com/aryaxt/iOS-Rich-Text-Editor

Related

Load internet links from HTML string

I am using the function (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
[webView loadHTMLString:sourceCode baseURL:nil];
to load some source-code I wrote, with links to internet content. For instance :
Google
The string load well, but there is no way to access the google link then.
Do you know what I am doing wrong ? Is there anything to do with the baseURL ?
your webpage must return a string that just contain http://www.google.com. If you need to use the link, you must modify the NSString that you receive to extract the right url!
Try setting the dataDetectorType for your webview like this:
webView.dataDetectorTypes = UIDataDetectorTypeLink;
Hope this helps!

text and image as one scrollable entity

I need an undefined amount of text and one or more pictures to be scrollable as one entity. I'm quite surprised that this doesn't seem to be provided by default, I thought I've seen that several times before... I tried to google, but all I find doesn't fit. The images won't be wider than the screen, but in between lines of text.
I need something that let's me do something like:
image
textA
textA goes on
__ screen ends here, content goes on
textA goes on
textA goes on
image
image
textB
textB goes on
image
textC
The content for the text would come out of a plist, but I THINK I can predict it will be REALLY static, so I could just set the Text in IB and create a view for every content -.-.
I've read about Web View, but as far as I got it, you'd need internet connection to make that work, and the app should work without any internet connection at all.
Any suggestions or experiences concerning that?
Thanks a lot!
There different way to get things done:
If you have static content and want a complicated layout and know how to do it in html you should go with UIWebview and a bundled html file and images and load it with something like:[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"MyStuff" ofType:#"html"]]]];
You can also do a layout with UILabels and UIImageviews and arrange all of this onto one UIScrollview
If it's more dynamic you should go with the latter, but you need to program sort of layout algorithms that handle different number/sizes of images, number/length of test paragraphs and so on.
One option, as you mention, is UIWebView.
It does not require a connection, since you can load a static HTML into it executing:
– loadHTMLString:baseURL:
By specifying a baseURL that "points" to your bundle, you can also include images as resources in your Xcode project and have them displayed (by using <img src=... /img in your HTML):
NSString* basePath = [[NSBundle mainBundle] bundlePath];
[_label loadHTMLString:text baseURL:[NSURL fileURLWithPath:basePath]];
You don't need an internet connection to make a web view work. Look at this method on UIWebView:
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL

UIWebview contents as NSString from HTML

I'm trying to reading part of HTML source code from website that has been encapsulated by javascript;basically i'm trying to read email which appears in webview but not in real source code using NSString method of 'NSString stringWithContentsOfURL:(NSURL *)url'. it looks like code showing up before hitting up the inner java script (which executes and shows the email address to show).
are there any way i can get into NSString the contents that I viewed over UIWebview?
I tried to use the method 'webView stringByEvaluatingJavaScriptFromString' it worked for only displaying through webview browser didn't return any string value.
Are there anyway I can get the string?
This should work in the general case:
NSString *markup = [webView stringByEvaluatingJavaScriptFromString:
#"document.getElementsByTagName('html')[0].outerHTML"];
Just make sure the page is loaded before you call it.

UITextView Rich text?

I was wondering if UITextView can display rich text? I want to have simple formatting on my read only text (like different alignment for different parts of the text). if not, what view should I use?
Generally, the best way to do this is to use a UIWebView and load local content in where you can insert tags and whatever else you need. Local loading should be fast and you can stylize it to look like any uitextview
Another option is to use a UILabel subclass instead and get the Three20 open-souce code. You want the TTStyledTextLabel.
Here is a code snippet for anyone trying to do this - use the uiwebview to handle styled text - getting your local js files to work can be trick. Just go into Build Phases -> Compile Sources -> and add the js files you want to access locally in your html content. You can also evaluate strings as javascript from objective c on your webview.
NSString *someHtmlContent = [NSString stringWithFormat:#"<html><head><style type=\"text/css\">body {padding: 0px; margin: 0px; }</style><script src=\"yourLocalJsFile.js\"></script></head><body></body></html>", dynamicText];
NSLog(#"HTML with code: \n%#", someHtmlContent);
NSString *resourcePath = [[[[NSBundle mainBundle] resourcePath]
stringByReplacingOccurrencesOfString:#"/" withString:#"//"]
stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
[MyWebView loadHTMLString:someHtmlContent baseURL:[NSURL URLWithString:
[NSString stringWithFormat:#"file:/%#//", resourcePath]]];
[self.MyWebView setDelegate:self];
Setting the delegate let's you decide how to handle links - do you want them to open in mobile safari or in your webview
You can have a look at the EGOTextView project on github. It discribes itself as a
"A complete drop in replacement for UITextView, adding support for
rich text editing."

Remove html tags from UIWebView

I am developing an application for iphone which needs to load some description to a UIWebView. But text that loads into the UIWebView shows some html tags like <p></p>.Is there any way to remove these type of tags?
Thanks in advance
I asked a similar question the other day and got some good responses. I imagine one of these could suit your needs.
anyway to delete all <a href=> tags with javascript after the page loads on an iPad?
Hope it is helpful!
This is the best answer and is exactly what you are looking for ...
Write the following script in the webView delegate method. ( UIWebviewdidfinishLoading)
NSString *myText = [webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.textContent"];