1

我将 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)
    }
}
4

1 回答 1

0

事实证明,我在 stringAttributes 的键中使用 NSFontAttributeName 作为字符串,而不是类型本身。删除引号解决了这个问题。

于 2015-03-13T00:56:56.200 回答