Bro Bro - 10 months ago 114
Swift Question

Swift load html file in webview, change image size, font and text size

Can anyone help me with add html file in webview? I want to present html data with my options of text size, font and image size.

I have this html code, it works when I add it in local file :

<style type="text/css">
img {
max-width: 348px !important;
}
body {
font-size: 15px !important;
font-size: HelveticaNeue !important;
}
</style>


My question is how add this html code to each file I load from URL with swift?

Answer Source

I think if you want to add these html code at the end of a file, it's no problem. But as I know, the style needs to add in head, so I think you must Analysis the html string and find where to insert the style. The the problem is seem to be a string stitching.

let path = dir.stringByAppendingPathComponent(file);

//reading
let text = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil)

// Here you should insert the <style> string in text
// Or you just add the <style> at the end of the text

//writing
text.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding, error: nil)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download