问题
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 ?
回答1:
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
}
回答2:
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>";
回答3:
Here is the swift 4 version of the HTMLImageCorrector:
func HTMLImageCorrector(HTMLString: String) -> String {
var HTMLToBeReturned = HTMLString
while HTMLToBeReturned.range(of: "(?<=width=\")[^\" height]+", options: .regularExpression) != nil{
if let match = HTMLToBeReturned.range(of:"(?<=width=\")[^\" height]+", options: .regularExpression) {
HTMLToBeReturned.removeSubrange(match)
if let match2 = HTMLToBeReturned.range(of:"(?<=height=\")[^\"]+", options: .regularExpression) {
HTMLToBeReturned.removeSubrange(match2)
let string2del = "width=\"\" height=\"\""
HTMLToBeReturned = HTMLToBeReturned.replacingOccurrences(of:string2del, with: "")
}
}
}
return HTMLToBeReturned
}
回答4:
Swift 3
extension String {
func HTMLImageCorrector() -> String {
var HTMLToBeReturned = self
while HTMLToBeReturned.range(of: "(?<=width=\")[^\" height]+", options: .regularExpression) != nil{
if let match = HTMLToBeReturned.range(of: "(?<=width=\")[^\" height]+", options: .regularExpression) {
HTMLToBeReturned.removeSubrange(match)
if let match2 = HTMLToBeReturned.range(of: "(?<=height=\")[^\"]+", options: .regularExpression) {
HTMLToBeReturned.removeSubrange(match2)
let string2del = "width=\"\" height=\"\""
HTMLToBeReturned = HTMLToBeReturned.replacingOccurrences(of: string2del, with: "style=\"width: 100%\"")
}
}
}
return HTMLToBeReturned
}
}
来源:https://stackoverflow.com/questions/37608366/swift-how-to-change-only-img-or-image-sizes-in-webview