簡體   English   中英

iOS,Swift:如何允許讀者使用導航欄按鈕項來調整WKWebView中的正文字體大小?

[英]iOS, Swift: How do I allow readers to use Navigation Bar Button Items to adjust body font size in a WKWebView?

在此處輸入圖片說明

問候。 我正在嘗試使用以編程方式創建的兩個導航欄按鈕項(上圖中的較小和較大的“ A”),以允許讀者在iPhone和iPad的應用程序中增加或減少WKWebView中顯示的文本大小。台iPad。

在視圖控制器中,我具有以下導入語句:

導入UIKit

導入WebKit

在viewDidLoad中,我具有以下用於創建兩個導航欄按鈕圖標的功能:

    let button1 = UIBarButtonItem(image: UIImage(named: "A_16x16"), style: .plain, target: self, action:#selector(decreaseFontSize))

    let button2 = UIBarButtonItem(image: UIImage(named: "A_28x28"), style: .plain, target: self, action:#selector(increaseFontSize))

    self.navigationItem.rightBarButtonItems = [button2, button1]

我還啟動了以下功能:

    func decreaseFontSize(_ button:UIBarButtonItem!) {

    fontSize = (fontSize > 14) ? fontSize-6 : fontSize
    let fontSpecifier = "document.body.style.fontSize = '" + String(fontSize) + "px'"
    print("decrease font size button pressed")
    print(fontSpecifier)
    textWebView.evaluateJavaScript(fontSpecifier, completionHandler: nil)

}

func increaseFontSize(_ button:UIBarButtonItem!) {

    fontSize = (fontSize < 50) ? fontSize+6 : fontSize
    let fontSpecifier = "document.body.style.fontSize = '" + String(fontSize) + "px'"
    print("increase font size button pressed")
    print(fontSpecifier)
    //textWebView.evaluateJavaScript("document.body.style.fontSize = '40px'", completionHandler: nil)
    textWebView.evaluateJavaScript(fontSpecifier, completionHandler: nil)

}

HTML文本的CSS包括以下樣式:

  body {
      color: black;
      background: white;
      font-size: 1.2em; /*20px; but use em */
      text-align: left;
      font-family: "MyCustomFont", sans-serif;
      margin-right: 8px;
      margin-left: 8px;
  }

這有些起作用,但是“ MyCustomFont”(或不帶引號的MyCustomFont)將被忽略。 我非常想顯示自定義字體。

任何指導或建議的解決方案將不勝感激。

感謝Aditya Deshmane對在WKWebView中使用自定義字體的回答, 將其放在CSS文件的頂部,並顯示了自定義字體:

    @font-face {
        font-family: 'MyCustomFont';
        src: local('MyCustomFont'),url('MyCustomFont.ttf') format('truetype');
    }

這些圖標似乎可以按照我的iPad mini 4的要求調整字體大小,但是在iPhone 6中“工作”有點不一致。由於存在空間,我尚未接受自己的答案作為“可接受的答案”。改進。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM