5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOSアプリから印刷させる(Swift2.1, XCode7.2)

Last updated at Posted at 2016-02-04

Swiftでアプリから文書を印刷させるためのメモ

var s = ""
s += "<FONT color='#00ff00'><h1>hogeeeeeeeeeee</h1></FONT>"
s += "<br><br>"
s += "<font size='10' face='Osaka'><h2>piyooooooooooooooo<br>foooo</h2><br></font>"
s += "<font size='10' face='arial'><h2>piyooooooooooooooo<br>foooo</h2><br></font>"
s += "<strong>ほほほほほげ</strong>"
s += "<small>うぬぬぬぬ</small>"


let pi = UIPrintInfo(dictionary: nil)
pi.outputType = UIPrintInfoOutputType.General
pi.jobName = "print Job"
pi.orientation = UIPrintInfoOrientation.Portrait
pi.duplex = UIPrintInfoDuplex.LongEdge

let pic = UIPrintInteractionController.sharedPrintController()
pic.printInfo = pi

// markupText(HTMLファイル)印刷させる場合
let formatter = UIMarkupTextPrintFormatter(markupText: s)

// text(文字列)印刷させる場合
// let formatter = UISimpleTextPrintFormatter(text: s)
// formatter.font = UIFont.systemFontOfSize(CGFloat(20)) //フォントサイズ
// formatter.color = UIColor.blueColor() //フォントカラー

formatter.contentInsets = UIEdgeInsets(top: 20, left: 32, bottom: 32, right: 20) //余白

pic.printFormatter = formatter
pic.presentAnimated(true, completionHandler: nil)

markupText(HTML)で印刷した結果
markupText.jpg

text(文字列)で印刷した結果
text.jpg

5
4
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?