Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

Text Alignment in CGRect using PDFKit - Swift 5 - Xcode 12

I'm trying to center text in a CGrect inside a PDF. I can generate the PDF and draw rects, but ive been looking for a method to draw certain text into certain rects, as the text is filled in by the user, i cant get it to dynamically stay centered. I feel ive run myself down a path and cant find a way back out. Can anyone shed any light or even suggest a fix?

This method produces an error for me

Cannot convert value of type 'CGRect' to expected argument type 'CGFloat'

Heres an example:

func addActNumber(pageRect: CGRect) -> CGFloat {

        let titleFont = UIFont.systemFont(ofSize: 18.0, weight: .bold)

        let titleAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: titleFont]
        let attributedTitle = NSAttributedString(string:"Act (actNumber)", attributes: titleAttributes)

        let ActNumberStringSize = attributedTitle.size()
        
        let ActNumberRect = CGRect (x: 10 , y: 35, width: 191.6, height: 25 )

        let titleStringRect = CGRect(x:  (ActNumberRect - ActNumberStringSize.width) / 2.0 , y: 35, width: 191.6 ,
                                     height: 25)
      

        attributedTitle.draw(in: titleStringRect)
        

        return titleStringRect.origin.y + titleStringRect.size.height
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...