Open makhocheung opened 1 year ago
Hi, @makhocheung thank you for using and providing feedback.
From the content of your issue, I have identified three problems that need to be resolved:
I'll resolve it as soon as possible.
Thank you again for your feedback, I have not fully tested on macOS or using SwiftUI yet.
After conducting research, I have concluded that AttributedString and NSAttributedString are not small objects and do not share the same inheritance. Therefore, I believe that using NSAttributedString in AttributedString or SwiftUI is not a good idea. Instead, we should create a new mapper to map ZMarkupParser.Markup objects to SwiftUI Kit (Text/Image) components, rather than using NSAttributedString directly in AttributedString.
Unfortunately, it may not be implemented quickly.
A temporary dirty solution would be to find ZNSTextAttachment for each AttributedString and manually trigger startDownload() and handle delegate for it.
content.compactMap({ $0.attachment as? ZNSTextAttachment }).forEach { attachment in
attachment.delegate = self
attachment.startDownlaod()
})
// handler delegate
func zNSTextAttachment(didLoad textAttachment: ZNSTextAttachment, to: ZResizableNSTextAttachment) {
//
replace textAttachment to to in AttributedString
}
I know it's very dirty, but as I mentioned earlier, it's not recommended to use it in SwiftUI currently.
Please let me know if there is anything I have misunderstood, as I am not very familiar with SwiftUI.
Currently,AttributedString supports markdown natively except ![]()
,so it's normal that AttributedString can't load remote images. Maybe Apple will add this feature in the future.
we should create a new mapper to map ZMarkupParser.Markup objects to SwiftUI Kit (Text/Image) components, rather than using NSAttributedString directly in AttributedString.
This is a good idea. Developers may hope ZMarkupParser supports SwiftUI.
Actually, I'm appreciate that you have added support of h1~h6. ZMarkupParser currently satisfies my project. I will give more feedback later. Thank you.
I'll add on here that this is really more of a failing of a failing of SwiftUI's Text()
than of this library. It is very simple to convert between the 2 nowadays.
let nsAttr = parser.render(html)
let attr = AttributedString(nsAttr)
The real issue is that AttributedString
and Text()
are missing a lot of functionality, most notably support for NSParagraphStyle
which you need to do paragraph level alignment, spacing, lineSpacing, justification, etc.
For my own projects I use a UIViewRepresentable
wrapping a UITextView
to get the maximum flexibility.
Here is the example.
Here is the error printed by console.