techprimate / TPPDF

TPPDF is a simple-to-use PDF builder for iOS and macOS written in Swift
https://techprimate.github.io/TPPDF/
MIT License
743 stars 125 forks source link

[DEVELOE MODE] - CUSTOM FONT - PAGE NUMBERING #54

Closed StefaniOSApps closed 6 years ago

StefaniOSApps commented 6 years ago

how can i set a custom font @ page numbering?

document.setFont(.footerRight, font: UIFont(name: "ArialMT", size: 12.0)!)

is not working

document.pagination = PDFPagination(container: .footerRight,
                                                style: PDFPaginationStyle.customClosure(
                                                    {(page, total) -> String in
                                                        return "\(page) | \(total)"
                                                }))
philprime commented 6 years ago

The string is converted into a text element using the following:

let simpleText = PDFSimpleText(text: pagination.style.format(page: currentPage, total: totalPages))
let textObject = PDFAttributedTextObject(text: simpleText)

I didn't think of the styling. Should we add custom styling properties to the pagination or just change it to simple text and let it use the footer style?

StefaniOSApps commented 6 years ago

Ok its works

let simpleText = PDFSimpleText(text: pagination.style.format(page: currentPage, total: totalPages))
let textObject = PDFAttributedTextObject(attributedText: PDFAttributedText(text:NSAttributedString(string: simpleText.text, attributes: [NSAttributedStringKey.font: UIFont(name: "ArialMT", size: 12)])))

Can you pass the font and font size when the page numbers are generated?

looks like

document.pagination = PDFPagination(container: .footerRight,
                                  font: UIFont(name: "ArialMT", size: 12),
                                  style: PDFPaginationStyle.customClosure(
                                  {(page, total) -> String 
                                                   in return "\(page) | \(total)"
                                  }))
philprime commented 6 years ago

I added the option to pass an a dictionary of attributes. You can now do the following:

document.pagination = PDFPagination(container: .footerRight, style: PDFPaginationStyle.customClosure() { (page, total) -> String in
            return "\(page) / \(total)"
            }, range: (1, 20), hiddenPages: [3, 7], textAttributes: [
            .font: UIFont.boldSystemFont(ofSize: 15.0),
            .foregroundColor: UIColor.green
        ])