Giter VIP home page Giter VIP logo

homeassessment's People

Watchers

Cheungbo Mong avatar

homeassessment's Issues

PDFKit 圖片排版

我參考了你的程式
文字編排沒有問題
目前我需要加入圖片也能夠使它自動編排

`func addimage(_ text: String,
font: UIFont,
in context: UIGraphicsPDFRendererContext,
at currentPage: Int,
from lastPosition: CGFloat = 0) -> (Int, CGFloat) {

        let textAttributes: [NSAttributedString.Key: Any] =
          [NSAttributedString.Key.font: font]

        let currentText = CFAttributedStringCreate(nil,text as CFString,textAttributes as CFDictionary)

        let framesetter = CTFramesetterCreateWithAttributedString(currentText!)
        var currentRange = CFRange(location: 0, length: 0)
        var currentPage = currentPage
        var done = false
        var lastPosition = lastPosition

           let maxHeight = pageRect.height * 0.2
           let maxWidth = pageRect.width * 0.3
           
           let aspectWidth = maxWidth / logo.size.width
           let aspectHeight = maxHeight / logo.size.height
           let aspectRatio = min(aspectWidth, aspectHeight)
           
           let scaledWidth = logo.size.width * aspectRatio
           let scaledHeight = logo.size.height * aspectRatio
            
           let imageX = (pageRect.width - scaledWidth) / 2.0
           let imageRect = CGRect(x: imageX, y: lastPosition,
                                  width: scaledWidth, height: scaledHeight)
           
           logo.draw(in: imageRect)
           
        repeat {
            let group = renderPage(currentPage, withTextRange: currentRange, andFramesetter: framesetter, from: lastPosition+maxHeight)
            
 
            currentRange = group.0
            lastPosition = group.1

     
            if currentRange.location == CFAttributedStringGetLength(currentText) {

                done = true
            } else {

                context.beginPage()
                currentPage += 1
                
                drawPageNumber(currentPage)
                lastPosition = 0
                
                cgContext.textMatrix = .identity
                cgContext.translateBy(x: 0, y: pageHeight)
                cgContext.scaleBy(x: 1, y: -1)
            }

            
          lastPosition = lastPosition + scaledHeight/aspectRatio

            
        } while !done

        return (currentPage, lastPosition)
    }`

但是這樣並不可行
圖片上下會顛倒
且圖片無法以lastPosition定位

您能給我一點幫助嗎?
我會非常感謝

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.