Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for the new AttributedString #262

Open
liang2kl opened this issue Jun 14, 2021 · 5 comments
Open

Support for the new AttributedString #262

liang2kl opened this issue Jun 14, 2021 · 5 comments

Comments

@liang2kl
Copy link

In WWDC 21 a new AttributedString type was introduced, useful for rendering attributed text in SwiftUI.

It seems that converting the NSAttributedString instance generated by the module to AttributedString using init<S>(_ nsStr: NSAttributedString, including scope: S.Type) throws where S : AttributeScope does not preserve any renderable attribute, as shown in the figure, where the top view is the attributed text:

IMG_7042

The code to generate the attributed string is:

let markdownString = try! Down(markdownString: text).toAttributedString()
let attributedString = try! AttributedString(markdownString, including: \.foundation)

To display:

Text(attributedString)

Maybe some fixes or directly generating the attributed string could help.

@johnxnguyen
Copy link
Owner

Hi @liang2kl , I haven't been able to look at this yet. Is this no longer an issue?

@liang2kl liang2kl reopened this Jun 18, 2021
@liang2kl
Copy link
Author

Hi @liang2kl , I haven't been able to look at this yet. Is this no longer an issue?

Sorry, seems I closed this by mistake.

I have experimented with this for a while, and the reason might be SwiftUI not recognizing the attributes created with UIKit
(the AttributedString instance do have the original attributes, though).

So I guess we need a styler for AttributedString?

@johnxnguyen
Copy link
Owner

I'm afraid I haven't yet had a look at the new AttributedString api, so I welcome any input that you have.

@liang2kl
Copy link
Author

liang2kl commented Jun 22, 2021

I have a solution after some exploration.

The problem is that, SwiftUI won't recognize any attribute from UIKit. Instead, it use different types, with limited attributing options.

So (it seems) the only solution is to convert the UIKit attributes to the SwiftUI ones, like:

var attrStr: AttributedString = ...

for run in attrStr.runs {
    if let font = run.uiKit.font {
        attrStr[run.range].swiftUI.font = Font(font as CTFont)
    }
}

This method can transform the correspond attribute to any currently supported attribute in SwiftUI, and I works. But as SwiftUI's attributing support is very poor, not every original attribute can be transformed and displayed. So I am afraid that there's no perfect solution to displaying Markdown in SwiftUI currently.

@johnxnguyen
Copy link
Owner

But as SwiftUI's attributing support is very poor, not every original attribute can be transformed and displayed.

@liang2kl I would wait until AttributedString matures enough to be able to support the standard TextKit attributes, otherwise, as you suggest, the experience will be quite poor. In any case, I'll return to this at some point to see how we can take advantage of new TextKit apis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants