Swift:如何仅更改< img>或webView中的图像大小 [英] Swift: How to change only <img> or image sizes in webView
问题描述
我从服务器获取HTML并将其显示在UIWebView中。由于要求,未选择适合缩放页面选项。 UIWebView的宽度等于屏幕宽度和高度因内容而异。 HTML中的图像标签包含一些内联样式,这些样式在webView的视图中产生了一些问题。
截图附件
在第一张图片中您可以看到图片很小,因为尺寸很小设置为内联样式。但第二个有更大的图像,所以图像超出了界限。
有什么办法可以控制图像的宽度等于屏幕宽度/ UIWebview宽度?
这里是我用来省略HTML中宽度和高度的代码。 uiwebview
然后在屏幕宽度内调整图像
func HTMLImageCorrector HTMLString:String) - > String {
var HTMLToBeReturned = HTMLString
HTMLToBeReturned.rangeOfString((?< = width = \)[^ \height] +,options:.RegularExpressionSearch)!= nil {
if 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
}
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
}
这篇关于Swift:如何仅更改< img>或webView中的图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!