我将 UIPrintPageRenderer 子类化为通过 AirPrint 绘制我自己的打印页面。我有一个简单的 String 对象,我想更改字体的大小。使用drawAtPoint withAttributes 方法,它的字符串被绘制在正确的位置,但字体名称和大小不变。是否可以在 UIPrintPageRenderer 的 drawPageAtIndex 方法中使用 NSAttributedStrings?
示例代码:
import UIKit
class MyPrintPageRenderer: UIPrintPageRenderer {
override func drawPageAtIndex(pageIndex: Int, inRect printableRect: CGRect) {
let font = UIFont(name: "Times", size: 72.0)!
var stringAttributes = [NSObject: AnyObject]()
stringAttributes["NSFontAttributeName"] = font
let lineOne = "Some text"
let lineOnePointX = CGRectGetMidX(printableRect) - nameLineOne.sizeWithAttributes(stringAttributes).width / 2
let lineOnePointY = CGRectGetMinY(printableRect)
let lineOnePoint = CGPoint(x: lineOnePointX, y: lineOnePointY)
lineOne.drawAtPoint(lineOnePoint, withAttributes: stringAttributes)
}
}