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
Comments
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 So I guess we need a styler for |
I'm afraid I haven't yet had a look at the new |
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. |
@liang2kl I would wait until |
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 toAttributedString
usinginit<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:The code to generate the attributed string is:
To display:
Maybe some fixes or directly generating the attributed string could help.
The text was updated successfully, but these errors were encountered: