Ios 使用CGPDFContextCreateWithURL时PDF中缺少文本
我正在尝试创建一个带有一些标题文本的简单pdf。我正在使用CGPDFContextCreateWithURL创建文档。由于某些原因,没有显示任何文本,但我可以让其他图形对象显示Ios 使用CGPDFContextCreateWithURL时PDF中缺少文本,ios,swift,pdf,core-graphics,Ios,Swift,Pdf,Core Graphics,我正在尝试创建一个带有一些标题文本的简单pdf。我正在使用CGPDFContextCreateWithURL创建文档。由于某些原因,没有显示任何文本,但我可以让其他图形对象显示 override func viewDidLoad() { super.viewDidLoad() drawSuperPDF("test4.pdf") showPDFFile() } 我的drawsupperdf()函数 My drawHeaders()和drawHeaderText()函数 func dra
override func viewDidLoad() {
super.viewDidLoad()
drawSuperPDF("test4.pdf")
showPDFFile()
}
我的drawsupperdf()函数
My drawHeaders()和drawHeaderText()函数
func drawHeaders() {
drawHeaderText(CGPoint(x: 30, y: 10), headerText: "Hello World:", underline: true)
drawHeaderText(CGPoint(x: 30, y: 25), headerText: "Hello World:", underline: true)
drawHeaderText(CGPoint(x: 30, y: 40), headerText: "Hello World:", underline: true)
drawHeaderText(CGPoint(x: 30, y: 70), headerText: "Hello World:", underline: true)
}
func drawHeaderText(point: CGPoint, headerText: String, underline: Bool) {
let drawingPoint = CGPoint(x: point.x, y: point.y)
if underline {
var multipleAttributes = [String : NSObject]()
multipleAttributes[NSFontAttributeName] = UIFont.boldSystemFontOfSize(12)
multipleAttributes[NSUnderlineStyleAttributeName] = NSUnderlineStyle.StyleSingle.rawValue
headerText.drawAtPoint(drawingPoint,
withAttributes: multipleAttributes)
} else {
let font = UIFont.systemFontOfSize(12)
headerText.drawAtPoint(drawingPoint,
withAttributes: [NSFontAttributeName : font])}
}
最后,我的showPdfile()
您好:)您没有显示简单的图形调用,但我敢打赌它们将图形上下文作为参数。。。“drawAtPoint”函数不接受图形上下文,因此它可能不知道您要在PDF文件中绘制。看看“UIGraphicsPushContext”;在你的文本调用之前调用它,看看这是否会改善情况:)当我尝试UIGraphicsPushContext时,我只是得到一个没有pdf的屏幕。我还修改了函数以将上下文传递给函数,但没有成功:(
func drawHeaders() {
drawHeaderText(CGPoint(x: 30, y: 10), headerText: "Hello World:", underline: true)
drawHeaderText(CGPoint(x: 30, y: 25), headerText: "Hello World:", underline: true)
drawHeaderText(CGPoint(x: 30, y: 40), headerText: "Hello World:", underline: true)
drawHeaderText(CGPoint(x: 30, y: 70), headerText: "Hello World:", underline: true)
}
func drawHeaderText(point: CGPoint, headerText: String, underline: Bool) {
let drawingPoint = CGPoint(x: point.x, y: point.y)
if underline {
var multipleAttributes = [String : NSObject]()
multipleAttributes[NSFontAttributeName] = UIFont.boldSystemFontOfSize(12)
multipleAttributes[NSUnderlineStyleAttributeName] = NSUnderlineStyle.StyleSingle.rawValue
headerText.drawAtPoint(drawingPoint,
withAttributes: multipleAttributes)
} else {
let font = UIFont.systemFontOfSize(12)
headerText.drawAtPoint(drawingPoint,
withAttributes: [NSFontAttributeName : font])}
}
func showPDFFile() {
let fileName = "test4.pdf"
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let pdfFileName = documentsURL.URLByAppendingPathComponent(fileName)
let fullPath = pdfFileName.path!
let webView : UIWebView = UIWebView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
let url : NSURL = NSURL(fileURLWithPath: fullPath)
let request : NSURLRequest = NSURLRequest(URL: url)
webView.scalesPageToFit = true
webView.loadRequest(request)
self.view.addSubview(webView)
}