【发布时间】:2016-12-21 09:57:24
【问题描述】:
我在旧的 Cocoa 项目中使用普通的 WebView 组件,并且可以覆盖
-(void)webView:(WebView *)sender printFrameView:(WebFrameView *)frameView
在必要时显示打印对话框。
如何使用 WKWebView 做同样的事情?我在文档中找不到任何关于打印的方法。
【问题讨论】:
我在旧的 Cocoa 项目中使用普通的 WebView 组件,并且可以覆盖
-(void)webView:(WebView *)sender printFrameView:(WebFrameView *)frameView
在必要时显示打印对话框。
如何使用 WKWebView 做同样的事情?我在文档中找不到任何关于打印的方法。
【问题讨论】:
我有以下方法在 WKWebView 上使用 Swift 进行 AirPrint(例如显示打印对话框)。它将弹出打印对话框,并在需要时跨多个页面正确格式化文本。我从应用程序上的“打印”按钮调用 printPage。
webViewX 是一个 WKWebView,在类的其他地方定义:
var webViewX: WKWebView!
//do all the work for populating the webViewX somewhere else in the class
@objc func printPage(sender: AnyObject)
{
let pInfo :UIPrintInfo = UIPrintInfo.printInfo()
pInfo.outputType = UIPrintInfo.OutputType.general
pInfo.jobName = self.title //page title defined elsewhere
pInfo.orientation = UIPrintInfo.Orientation.portrait
let printController = UIPrintInteractionController.shared
printController.printInfo = pInfo
printController.printFormatter = webViewX.viewPrintFormatter()
printController.present(animated: true, completionHandler: nil)
}
【讨论】: