【发布时间】:2016-05-22 18:43:45
【问题描述】:
所以我正在尝试使用 swift 生成 PDF。我一直在尝试从 Objective C 转换为 swift,现在有一些我不知道如何修复的错误。
代码如下:
@IBAction func createPDFOnClick(sender: AnyObject!)
{
pageSize = CGSizeMake(850,1100)
let fileName: String = "makeME.pdf"
var path: [AnyObject] = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,true)
let documentDirectory: String = path[0] as! String
let pdfPathWithFileName: String = documentDirectory.stringByAppendingString(fileName)
self.createPDF(pdfPathWithFileName)
}
func createPDF(filePath: String){
UIGraphicsBeginPDFContextToFile(filePath,CGRectZero,nil)
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0,0,850,1100),nil)
self.drawBackground()
self.drawText()
UIGraphicsEndPDFContext()
}
func drawBackground() {
let context: CGContextRef = UIGraphicsGetCurrentContext()!
let rect: CGRect = CGRectMake(0,0,pageSize!.width,pageSize!.height)
CGContextSetFillColorWithColor(context,UIColor.whiteColor().CGColor)
CGContextFillRect(context,rect)
}
func drawText() {
var context: CGContextRef = UIGraphicsGetCurrentContext()!
CGContextSetFillColorWithColor(context,UIColor.blackColor().CGColor)
var textRect: CGRect = CGRectMake(0,0,[[self myTextView]frame].size.with, self.myTextView.frame.size.height)
var myString: String = self.myTextView.text!
myString.drawInRect(textRect)
}
我的错误是以下几行:
var 路径:[AnyObject] = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)
我收到错误“使用未解析的标识符”
var textRect: CGRect = CGRectMake(0,0,[[self myTextView]frame].size.with, self.myTextView.frame.size.height)
这里我得到“期望分隔符”
提前致谢。
【问题讨论】: