代码之家  ›  专栏  ›  技术社区  ›  Valerio Santinelli

iOS打印包含图像的nsattributed字符串作为PDF的NSTextAttachement

  •  1
  • Valerio Santinelli  · 技术社区  · 6 年前

    我正在尝试转换 UITextView PDF格式。这个 超文本视图 使用 NSAttributedString 包含文本和图像的。 这些图像是 NSTextAttachment .

    我经历了 UIPrintPageRenderer 路过 UIPrintFormatter 我从 超文本视图 通过它 viewPrintFormatter() 功能和它的工作完美的文本,但我看不到任何图像。看起来图像实际上占用了空间,因为我可以看到文本行之间的空白,但是没有图像被渲染。

    我怀疑它可能是格式化程序,但我不知道如何解决它。 任何帮助都非常感谢。

    我用来从 NSAttributedString 具体如下:

    public func createPDF(text: NSAttributedString, documentPath: URL, customPrintFormatter: UIPrintFormatter? = nil) {
        let printFormatter = customPrintFormatter == nil ? UISimpleTextPrintFormatter(attributedText: text) : customPrintFormatter!
    
        let renderer = UIPrintPageRenderer()
        renderer.addPrintFormatter(printFormatter, startingAtPageAt: 0)
    
        // A4 size
        let pageSize = CGSize(width: 595.2, height: 841.8)
    
        // create some sensible margins
        let pageMargins = UIEdgeInsets(top: 72, left: 72, bottom: 72, right: 72)
    
        // calculate the printable rect from the above two
        let printableRect = CGRect(x: pageMargins.left, y: pageMargins.top, width: pageSize.width - pageMargins.left - pageMargins.right, height: pageSize.height - pageMargins.top - pageMargins.bottom)
    
        // and here's the overall paper rectangle
        let paperRect = CGRect(x: 0, y: 0, width: pageSize.width, height: pageSize.height)
    
        renderer.setValue(NSValue(cgRect: paperRect), forKey: "paperRect")
        renderer.setValue(NSValue(cgRect: printableRect), forKey: "printableRect")
    
        let pdfData = NSMutableData()
    
        UIGraphicsBeginPDFContextToData(pdfData, paperRect, nil)
        renderer.prepare(forDrawingPages: NSMakeRange(0, renderer.numberOfPages))
    
        let bounds = UIGraphicsGetPDFContextBounds()
    
        for i in 0  ..< renderer.numberOfPages {
            UIGraphicsBeginPDFPage()
    
            renderer.drawPage(at: i, in: bounds)
        }
    
        UIGraphicsEndPDFContext()
    
        do {
            try pdfData.write(to: documentPath)
        } catch {
            print(error.localizedDescription)
        }
    }
    

    下面是创建 NSTextAttachment公司

            let attachment = NSTextAttachment()
            attachment.image = UIImage.init(named: "1.png")
            attachment.bounds = CGRect.init(x: 0, y: 0, width: 200, height: 100)
            let img = NSMutableAttributedString.init(attachment: attachment)
    
            let imgRange = NSRange.init(location: match.range(at: 0).location + match.range(at: 0).length, length: img.length)
            self.beginEditing()
            backingStore.insert(img, at: match.range(at: 0).location + match.range(at: 0).length)
            let r = NSMakeRange(match.range(at: 0).location + match.range(at: 0).length, img.length)
            self.edited([.editedCharacters, .editedAttributes], range: range, changeInLength: img.length)
            self.endEditing()
    
    0 回复  |  直到 6 年前