cheungbo-mong / homeassessment Goto Github PK
View Code? Open in Web Editor NEWShare
Share
我參考了你的程式
文字編排沒有問題
目前我需要加入圖片也能夠使它自動編排
`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定位
您能給我一點幫助嗎?
我會非常感謝
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.