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."
Related
I am working on a small app, and I would like to bundle some short text descriptions, maybe with some images, in the help section. What control should I use to place these content? UILabel doesn't look too smart because it doesn't wrap text.
I used a UIWebView for this sort of thing.
I write out the help in HTML, including images if needs be, and then load it into a UIWebView. It is pretty flexible in terms of layout and fairly trivial to implement:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Instructions"
ofType:#"html"]
isDirectory:NO]]];
Just add the file Instructions.html to your app and you're good to go.
I want to display local html file that has css too in UIWebView. I am able to do that using following code..
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *path = [[NSBundle mainBundle] pathForResource:#"info" ofType:#"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:path];
[webView loadHTMLString:htmlString baseURL:baseUrl];
I am not able to get hover effects and my understanding is my be there is no hover kind of thing in a touch mobile,
Any ways my main issue is I had deleted my index.html from resources and then re added the updated one but not getting the updated one..
I don't know what happen the web view still loads the old html file. I tried to clean project too.
Second thing can I get css effects in iphone..?
Thanks..
As far as I know you can only achieve this result by using javascript-events like ontouchstart (or something).
I believe if you reset your simulator (While running your simulator, go to iOS Simulator > Reset Content and Settings in the menu bar) then re-run your app, the UIWebView will load the new index.html. UIWebViews cache these things, so you can add code to clear that cache and/or ignore the cache. Check here: Clearing UIWebview cache
I am trying to load the contents of a rtf file in the UIWebView. I am successful in loading the contents, but it is unformatted. the fonts are too large and it doesn't have any format. it displays the color of the rtf content, but not the font or size.
So what should i do now. is there any other way to load the rtf and format it? I load the rtf in following way:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Home" ofType:#"rtf"];
NSURL *url=[NSURL fileURLWithPath:path];
NSURLRequest *req=[NSURLRequest requestWithURL:url];
[menuWeb loadRequest:req];
So what should i do now?
Use HTML instead, it's the only way you're going to get the control you want.
http://cutesoft.net/example/editRTF.aspx
This page is a WYSIWYG HTML generator. It generates extremely clean HTML (*), which I can then save and insert in my project. I can then modify this HTML file from within Xcode. All very nice!
(*) provided you use it right... flip between the normal view, HTML view and final view -- don't make it export into the second text box otherwise everything comes out in an impenetrable chunk.
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
I was using NYTimes iPhone application, I become bit curious when I notice that it cache UIwebview even though I didn't open that article.Does anyone have idea how to do that?
How NYTimes iPhone application doing offline reading?
Any help greatly appreciated.
Thanks,
Amit
I wrote the NYTimes app. Here are some details that you could have gotten by looking inside the app bundle.
I download the HTML for the articles, strip out whatever unsupported HTML and JS crud the producers stuffed in it and cache it in the backing store.
The article content is contained in a series of P tags (HTML fragment). I stuff that into a special HTML skeleton page that ships with the app. The static wrapper page also contains CSS and JS used to properly display the article and lazily load the images.
The image loading is really cool. The web view is notified when the images are ready. layout is not affected because I already know the sizes of the missing images.
You can use UIWebView to load html files stored locally on the iPhone.
NYTimes app is probably caching html and images in local storage.
Search google for "UIWebView local" and you get several useful hits.
I tried it out and it works great:
First, create a "view based application" and add a UIWebView to the NIB.
Second, add this code to your UIViewController code:
- (void)viewDidLoad
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path isDirectory:NO];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[super viewDidLoad];
}
Third, add a file called "index.html" to your "Resources" folder in xcode and it will be displayed.
UPDATE:
Indeed, the complicated part of this is downloading the images and stylesheets for the webpage. Doing this server side is easy with Simple HTML Parser (and PHP). Just package everything in a zip and download to your iPhone.
Alternatively, you could do it locally with a C/C++/OBJC HTML parser (libxml2.2 is available on iOS). See this SO question Parsing HTML on the iPhone.
It's going to a bit of a project, so good luck.