在webview中快速加载html文件,更改图像大小,字体和文本大小

时间:2017-08-17 08:36:53

标签: html ios swift webview

任何人都可以帮我在webview中添加html文件吗?我想用我的文本大小,字体和图像大小选项来呈现html数据。

我有这个HTML代码,当我在本地文件中添加它时,它可以工作:

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

我的问题是如何将这个HTML代码添加到我使用swift从URL加载的每个文件中?

1 个答案:

答案 0 :(得分:1)

我认为如果你想在文件的末尾添加这些html代码,那就没问题了。但据我所知,样式需要添加 head ,所以我认为你必须分析 html字符串并找到插入<的位置强>式即可。问题似乎是字符串拼接。

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)