Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make fit size only for Image in UIWebView?

I loaded HTML to my UIWebView that including both Text and Images.

Text is fit to UIWebView and Images are not fit to UIWebView.

It's too large in UIWebView.

It's showing scroll bar at the end of the UIWebView.

I want to adjust image fit size in UWebView. So i tested with following codes.

self.myWebView.scalesPageToFit = YES;

However it's fixed including Text and Text is too small to read and Images are fixed.

I want to set Text Normally and only want to fit size Image in UIWeView.

How can i do it?

like image 918
Fire Fist Avatar asked Mar 10 '14 06:03

Fire Fist


1 Answers

I have solve this problem using css. The css contained within the style tags below will make sure to automatically resize your images maintaining aspect ratio for the width of the UIWebView. Hope this helps!

NSString *strTemplateHTML = [NSString stringWithFormat:@"<html><head><style>img{max-width:100%%;height:auto !important;width:auto !important;};</style></head><body style='margin:0; padding:0;'>%@</body></html>", @"insert your html content here"];

[webView loadHTMLString:strTemplateHTML baseURL:nil];
like image 126
ajay_nasa Avatar answered Oct 13 '22 11:10

ajay_nasa