ios開發之Swift使用AirPrint進行列印

來源:互聯網
上載者:User

使用 AirPrint 可以輕鬆地從 iOS 和 OS X app 中傳輸無損照片和文稿列印。當然,印表機也要支援AirPrint 技術才行。下面通過範例示範如何在App中使用 Airprint進行列印。 

 
1,印表機模擬器(Printer Simulator)下載

如果沒有支援AirPrint的印表機也沒關係,蘋果提供了個虛擬印表機,地址:https://developer.apple.com/downloads

(1)下載裡面的 Hardware IO Tools
 
(2)運行工具包裡的 Printer Simulator
 
(3)用瀏覽器開啟個網頁,使用模擬器列印下看看效果 
如果手機和電腦在同一個網段下,使用手機也是可以找到電腦上的印表機模擬器的。
下面是列印效果,可以看到虛擬印表機產生的是一個pdf的檔案,這個和實際列印出來的效果是一致的。
 
2,使用AirPrint列印一段文字
(1)列印時會彈出頁面(左圖)讓你選擇印表機,列印份數等。下面會顯示預覽效果
(2)右圖是最終列印出來的檔案

    

 代碼如下 複製代碼
import UIKit
 
class ViewController: UIViewController {
 
    override func viewDidLoad() {
        super.viewDidLoad()
    }
 
    @IBAction func printText(sender: AnyObject) {
        // 列印控制器
        let printController = UIPrintInteractionController.sharedPrintController()
        // 列印任務相關資訊
        let printInfo = UIPrintInfo(dictionary:nil)
        printInfo.outputType = UIPrintInfoOutputType.General
        printInfo.jobName = "my print Job"
        printController.printInfo = printInfo
         
        //設定列印文本
        let printText = "<div style='font-size:28px;font-weight:bold;text-align:center'>"
            + "hangge.com</div>"
            + "做最好的開發人員知識平台"
         
        // 格式化列印文本
        let formatter = UIMarkupTextPrintFormatter(markupText: printText)
        // 設定頁面Insets邊距
        formatter.contentInsets = UIEdgeInsets(top: 72, left: 72, bottom: 72, right: 72)
        printController.printFormatter = formatter
         
        // 提供列印介面讓使用者選擇印表機和副本的數量
        printController.presentAnimated(true, completionHandler: nil)
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}


好了上文就是111cn小編為各位整理的關於Swift使用AirPrint進行列印的例子,希望這篇文章能夠對各位帶來協助的哦。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.