How to set custom font in UIWebView? - iphone

I have a font in the resources and I loaded it into a UIFont
I want to know how to use this UIFont as the default font for my UIWebView
Thanks in advance...

If you are using webpage to display in the WebView
Here is what I would do :
Create a string with the header of the HTML page
add css file or embedded CSS in header
Create the body of HTML.
Create ending elements of HTML
Append all the strings and load WebView from string
NSString *bundlepath = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:bundlepath];
[webView loadHTMLString:html baseURL:baseURL];

This might help you out:
iPhone Development - Setting UIWebView font

Related

how to scale to fit pages opened from HTML file containing web links

I have an app I am working on in Xcode. Part of the app area loads an html file. The HTML file itself has some links to external websites. They open fine, except they are not scaled to fit on the iPhone.
How would I go about ensuring all web pages opened are scaled to fit? The code from the .m file is below
NSString *fileString = [[NSBundle mainBundle] pathForResource: #"chapter5" ofType: #"html"];
NSURL *newURL = [[NSURL alloc] initFileURLWithPath: fileString];
NSURLRequest *newURLRequest = [[NSURLRequest alloc] initWithURL: newURL];
[webview loadRequest: newURLRequest];
The HTML file "chapter5.html contains links to websites.
I thought I would try entering
webview.scalesPageToFit = YES
in the .m file hoping it would apply to anything opened, but that didn't work.
Any advice would be greatly appreciated.
Thanks
The scalesPageToFit will take effect only when the webView loads the request. Implement the delegate of UIWebView webViewDidFinishLoad: method and try this code.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGSize defaultSize = webView.frame.size;
CGSize fittingSize = [tableViewCell.webView sizeThatFits:defaultSize];
[webView sizeThatFits:fittingSize];
NSLog(#"Size : %#",NSStringFromCGSize(fittingSize));
}
Please set webview mode as Aspect-Fit
Thanks guys, I set webview mode as Aspect-Fit and set the Scaling to scale pages to fit and this worked, not sure how I missed that.

How to load large size PDF scroll view in iOS?

I have pdf of size 53MB when i try to load only one page of PDF in scrollview i got memory warning and then application crash.
Display the PDF in a UIWebView:
NSString *PDFPath = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"pdf"];
NSURL *URL = [NSURL fileURLWithPath:PDFPath];
NSURLRequest *URLRequest = [NSURLRequest requestWithURL:URL];
[webView loadRequest:URLRequest];
The WebView will load pages into memory as they appear on screen rather than all at once.
This is a duplicate of What is the best way to view large PDFs on iOS?
The accepted answer is to use a UIWebView as indicated above.
You should take a look at the vfr/Reader, which is a PDF Reader Core for iOS.
UIWebView is not very good in handling big files out of the box.

display image in html in webview from document directory

I would like to display an image in html which is being displayed on a UIWebView from the App's Document directory.
Can someone suggest me how to accomplish that?
This can help you
Using HTML and Local Images Within UIWebView
Link to resources inside WebView - iPhone
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
You can then refer to your images like this:
<img src="myimage.png">

How to get path to add number of Images that are in the resource to be displayed in the WebView in objective-c

I am added image to my application but I want to display them into my web view. I tried but not succeeded. how can I set the path for those images in html in image tag and how to display them.
Please help me out of this.
Thank you,
Madan Mohan.
Are you just displaying the images individually or do you have an HTML file that references them?
To get a URL to the files directly:
NSString *pathForImage = [[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"];
NSURL *URL = [NSURL fileURLWithPath:pathForImage];
If you're loading an HTML file that references the images (such as by including an <img src="image.png">, with no path info) then:
[webView loadHTMLString:htmlString
baseURL:[[NSBundle mainBundle] resourceURL]];
EDIT: so, supposing you had image.png in your application bundle as a resource, then you could do:
[webView loadHTMLString:#"<html><body><img src=\"image.png\"></body></html>"
baseURL:[[NSBundle mainBundle] resourceURL]];
In your HTML for the webview do
<img src="imagename">
Where imagename is the filename of the image. The iPhone doesn't have any folder structures or anything like that. That means that even if you have groups or folders set up in XCode, at runtime they are all lumped into one, giant directory.
If that doesn't work right away, look at what your are setting your base url to be. It might be that you need to set the baseURL to nil to remind the phone to look locally.
The following code sample might fix the problem for you.. Good luck!
NSData * image = [[NSMutableData alloc] initWithContentsOfFile:#"image.png"];
[self.webView loadData:image MIMEType:#"image/png" textEncodingName:nil baseURL:nil];
[image release];
[self.view addSubView:webView];

How to check the URL of a WebView?

I have a Subview that load a Webview. In this Webview I load a file HTML locally in the Documents folder of my App. I need to check if the webview load another HTML file. Specifically:
Webview load "index.html" locally in my Documents folder
After 7 days a Javascript load another page with location.href= "index2.html",
When I load this WebView I need to check if the current URL is index.html or index2.html, if is the second I must change Subview!
How can I do this?
NSString *currentUrl = [[[yourWebView request] URL] absoluteString];
Use this NSURL method [NSURL fileURLWithPath:#"some/path/to/your/file" isDirectory:NO] and pass that to a NSURLRequest which is then passed to your UIWebView. You can get the path to your resource by using the [[NSBundle mainBundle] pathForResource...] methods.
NSURL *url = myWebView.request.URL;