我正在子类化UIPrintPageRenderer以绘制自己的页面,以便通过AirPrint打印。我有一个简单的字符串对象,我想改变字体的大小。使用drawatPointWithAttributes方法,它将在正确的位置绘制字符串,但字体名称和大小不变。是否可以在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)
    }
}

最佳答案

结果是我在stringAttributes键中将nsfonttributename用作字符串,而不是类型本身。去掉引号就解决了这个问题。

关于ios - 在自定义UIPrintPageRenderer中使用UIFont绘制字符串对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29019694/

10-11 14:50