I am getting HTML from a server and display it in a UIWebView.The "Scale Page to fit" option is not selected because of the requirement. The UIWebView width is equal to the screen width and height varies according to the content. The image tags in HTML contains some inline styling which are creating some problem in the view of webView. Screenshots are attached
in 1st Image you can see the image is small because of small size set to it's inline styling. But 2nd one has larger image, so the image goes out of bound.
Is there any way to control the width of the image to equal to the screen width/ UIWebview width ?
Here is the code that I used to omit the widths and heights in HTML. The uiwebview
then adjusted the images within the screen width
func HTMLImageCorrector(HTMLString: String) -> String {
var HTMLToBeReturned = HTMLString
while HTMLToBeReturned.rangeOfString("(?<=width=\")[^\" height]+", options: .RegularExpressionSearch) != nil{
if let match = HTMLToBeReturned.rangeOfString("(?<=width=\")[^\" height]+", options: .RegularExpressionSearch) {
HTMLToBeReturned.removeRange(match)
if let match2 = HTMLToBeReturned.rangeOfString("(?<=height=\")[^\"]+", options: .RegularExpressionSearch) {
HTMLToBeReturned.removeRange(match2)
let string2del = "width=\"\" height=\"\""
HTMLToBeReturned = HTMLToBeReturned.stringByReplacingOccurrencesOfString( string2del, withString: "")
}
}
}
return HTMLToBeReturned
}
You can add style to your HTML body :
let styleContent = "<html><head><style>img{width:100%;};</style></head>"
+ "<body style='margin:0; padding:0;'>" + (your html body) + "</body></html>";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With