UIWebView and local css file

后端 未结 4 533
盖世英雄少女心
盖世英雄少女心 2020-12-13 03:10

I want to style a web page meant for the desktop so that it is presentable on a UIWebView on iPhone. I do not have access to the web server from which the pages originate. I

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 03:57

    SWIFT 3

    If you (like me) only use a html snippet (not a complete web page) you need to add your local css file in the header of the html snippet.

    I therefore define a html header like this:

    let htmlHeader = " \n  \n  \n  \n  \n"
    

    and a footer like this:

    let htmlFooter = " \n "
    

    Then find the baseURL for the css like this:

    let baseURLForCss = Bundle.main.url(forResource: "default", withExtension: "css")
    

    And load the html snippet with your own css like this:

    webView.loadHTMLString(String.init(format: "%@%@&@", htmlHeader, htmlSnippet, htmlFooter), baseURL: baseURLForCss)
    

提交回复
热议问题