Skip to content

Add support for Swift's new AttributedString #421

Description

@WhisperCF

ℹ Please fill out this template when filing an issue.
All lines beginning with an ℹ symbol instruct you with what info we expect. You can delete those lines once you've filled in the info.

Feature request should extend the framework. If you found a bug, please issue a Bug Report

How should the feature work?

PDFAttributedText currently only accepts NSAttributedStrings. Swift's current bridging from AttributedString to NSAttributedString supports NSFont and NSColor, etc., but also supports SwiftUI's Font and Color. These attributes are currently ignored by TPPDF.
PDFAttributedText(text: NSAttributedString) could have a cousin PDFAttributedText(text: AttributedString) that understands the newer Color and Font attributes.

What should it look like?

let document = PDFDocument(format: .usLettter)

var swiftyAttributedText = AttributedString("My Swifty string")
swiftyAttributedText.font = Font.bold
swiftyAttributedText.foregroundColor = Color.blue

document.add(attributedText: swiftyAttributedText)

...

output PDF has blue bold text "My Swifty string"

What happened instead?

Instead, when converting a Swift AttributedString to NSAttributedString, the newer formatting is ignored. Only if NSFont and NSColor attributes are supplied to AttributedString does converting to NSAttributedString result in the expected formatting in the final PDF. Furthermore, NSAttributedString does not conform to Sendable (and likely won't), while AttributedString does. This will be an error in Swift 6.

TPPDF Environment

TPPDF version: 2.6.1
Xcode version: 26.2
Swift version: 5 or 6

Demo Code / Project

See possible examples above.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions