How to make suitable size only for image in UIWebView?

I uploaded HTMLto mine UIWebView, which includes both Text, and Images.

TextSuitable for UIWebViewand Imagesnot suitable for UIWebView.

He is too big in UIWebView.

It shows the scroll bar at the end UIWebView.

I want to adjust image size in UWebView. So I tested the following codes.

self.myWebView.scalesPageToFit = YES;

However, it is fixed, including text and text are too small to read and images are fixed.

I want to set the text as usual and only want to fit the image in UIWeView.

How can i do this?

+4
source share
3

css. Css, , , UIWebView. , !

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];
+7

viewport UIWebView iPhone/iPad, , -.

 <meta name="viewport" content="width=device-width, maximum-scale=1.0" />

. :

https://developer.apple.com/library/ios/DOCUMENTATION/AppleApplications/Reference/SafariWebContent/UsingtheViewport/UsingtheViewport.html

+5

NSString *fontString = @"<!DOCTYPE html><html>\
    <head>\
    <style>\
    img{";
        NSString *imageDimensionsStr = [NSString stringWithFormat:@"max-width: %fpx; max-height: %fpx;}",self.view.frame.size.width - 50.0f, self.view.frame.size.height/2];
        fontString =[fontString stringByAppendingString:imageDimensionsStr];
fontString = [fontString stringByAppendingString:@"</style></head><body>"];
    fontString = [[fontString stringByAppendingString:htmlString] stringByAppendingString:@"</body></html>"];
    [webView loadHTMLString:fontString baseURL:nil];

. .

-1
source

Source: https://habr.com/ru/post/1530921/


All Articles