首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS,Swift:如何允许读者使用导航栏按钮项来调整WKWebView中的字体大小?

iOS,Swift:如何允许读者使用导航栏按钮项来调整WKWebView中的字体大小?
EN

Stack Overflow用户
提问于 2017-06-17 20:38:58
回答 1查看 249关注 0票数 0

欢迎光临。我试图使用两个以编程方式创建的导航栏按钮项(上面图像中的“A”越小越大),允许读者在WKWebView中为iPhones和iPads应用程序中显示的文本大小增加或缩小。

在视图控制器中,我有以下导入语句:

导入UIKit

导入WebKit

在viewDidLoad中,我有以下两个导航栏按钮图标:

代码语言:javascript
复制
    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]

我还启动了以下功能:

代码语言:javascript
复制
    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包括以下样式:

代码语言:javascript
复制
  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)被忽略了。I非常希望显示自定义字体。

如有任何指导或建议的解决办法,将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2017-06-20 22:07:00

由于Aditya对Using custom fonts in WKWebView的回答,我将其放在CSS文件的顶部,并显示了我的自定义字体:

代码语言:javascript
复制
    @font-face {
        font-family: 'MyCustomFont';
        src: local('MyCustomFont'),url('MyCustomFont.ttf') format('truetype');
    }

图标看起来像我的iPad一样调整字体大小--迷你4,但在我的iPhone 6中“工作”有点不一致。我还没有接受这个,我自己的答案,作为“接受的答案”,因为还有改进的余地。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44609194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档