【问题标题】:How to draw text in PDF context in Swift?如何在 Swift 中的 PDF 上下文中绘制文本?
【发布时间】:2015-11-13 19:31:35
【问题描述】:

我有一个创建 pdf 文件并返回其路径的简单函数。

func createPDFFileAndReturnPath() -> String {

    let fileName = "pdffilename.pdf"
    let paths = NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)
    let documentsDirectory = paths[0]
    let pathForPDF = documentsDirectory.stringByAppendingString("/" + fileName)

    UIGraphicsBeginPDFContextToFile(pathForPDF, CGRectZero, nil)

    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 100, 400), nil)

    let text = "text" //how to print this in whatever place?

    //text.drawInRect - this doesn't work

    UIGraphicsEndPDFContext()

    return pathForPDF
}

【问题讨论】:

    标签: swift ios8 cgpdfcontext


    【解决方案1】:

    这是你的功能:

    func createPDFFileAndReturnPath() -> String {
    
        let fileName = "pdffilename.pdf"
        let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        let documentsDirectory = paths[0] as! NSString
        let pathForPDF = documentsDirectory.stringByAppendingString("/" + fileName)
    
        UIGraphicsBeginPDFContextToFile(pathForPDF, CGRectZero, nil)
    
        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 100, 400), nil)
    
        let font = UIFont(name: "Helvetica Bold", size: 14.0)
    
        let textRect = CGRectMake(5, 3, 125, 18)
        var paragraphStyle:NSMutableParagraphStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as! NSMutableParagraphStyle
        paragraphStyle.alignment = NSTextAlignment.Left
        paragraphStyle.lineBreakMode = NSLineBreakMode.ByWordWrapping
    
        let textColor = UIColor.blackColor()
    
        let textFontAttributes = [
            NSFontAttributeName: font!,
            NSForegroundColorAttributeName: textColor,
            NSParagraphStyleAttributeName: paragraphStyle
        ]
    
        let text:NSString = "Hello world"
    
        text.drawInRect(textRect, withAttributes: textFontAttributes)
    
        UIGraphicsEndPDFContext()
    
        return pathForPDF
    }
    

    【讨论】:

    • paragraphStyle.lineBreakMode = NSLineBreakMode.ByWordWrapping 到底是做什么的?
    【解决方案2】:

    塞巴斯蒂安的回答 - 为 Swift 4.2 更新

    func createPDFFileAndReturnPath() -> String {
    
        let fileName = "pdffilename.pdf"
        let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
        let documentsDirectory = paths[0] as NSString
        let pathForPDF = documentsDirectory.appending("/" + fileName)
    
        UIGraphicsBeginPDFContextToFile(pathForPDF, CGRect.zero, nil)
    
        UIGraphicsBeginPDFPageWithInfo(CGRect(x: 0, y: 0, width: 100, height: 400), nil)
    
        let font = UIFont(name: "System", size: 14.0)
    
        let textRect = CGRect(x: 5, y: 3, width: 125, height: 18)
        let paragraphStyle:NSMutableParagraphStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
        paragraphStyle.alignment = NSTextAlignment.left
        paragraphStyle.lineBreakMode = NSLineBreakMode.byWordWrapping
    
        let textColor = UIColor.black
    
        let textFontAttributes = [
            NSAttributedString.Key.font: font!,
            NSAttributedString.Key.foregroundColor: textColor,
            NSAttributedString.Key.paragraphStyle: paragraphStyle
        ]
    
        let text:NSString = "Hello world"
    
        text.draw(in: textRect, withAttributes: textFontAttributes)
    
        UIGraphicsEndPDFContext()
    
        return pathForPDF
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-18
      • 2022-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多