Skip to content
This repository has been archived by the owner on May 6, 2023. It is now read-only.

NSAttributedString and HTML support #15

Closed
jgodonQobuz opened this issue Dec 16, 2022 · 1 comment
Closed

NSAttributedString and HTML support #15

jgodonQobuz opened this issue Dec 16, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@jgodonQobuz
Copy link

jgodonQobuz commented Dec 16, 2022

Hi !

First of all, thanks for this SwiftUI multi-line TextView implementation, that's great to be able to use it as easy as it was under UIKit ;)

I've a question, and maybe I've missed something... but when I use this Textview with an NSAttributedString containing some HTML, style is not updated but Markups are gone...

Maybe I'm doing something wrong...

Do you know if this has already worked for you ?

I mean something like:

// and then in my view

struct MyView: View {
 
  var content: String = "This is a <b>bold</b> text."
 
  var body: some View {
      if let contentHtml = content.htmlAttributedString {
          TextView(.constant(contentHtml))
              .isEditable(false)
              .isSelectable(false)
              .allowsRichText(true)
      }
  }
}

When View is displayed, I would expect the word bold to be in bold but here, <b> and </b> markups are removed but no bold anywhere...

Have any idea about that ?

Thank for your answer and have a nice weekend

Jérémie

// somewhere in my code (in a viewModel and in a String extension for example)

extension String {
    func htmlAttributedString() -> NSAttributedString? {
        guard let data = self.data(using: .utf8) else {  return nil  }

        return try? NSAttributedString(
            data: data,
            options: [.documentType: NSAttributedString.DocumentType.html],
            documentAttributes: nil
        )
    }
}
@jgodonQobuz jgodonQobuz added the bug Something isn't working label Dec 16, 2022
@shaps80
Copy link
Contributor

shaps80 commented Dec 19, 2022

Yeah I don’t believe this view currently supports AttributedString’s actually. I have seen several issues related to this.

I have a complete re-writ in-progress atm and that should resolve these kinds of issues.

That being said HTML support with attributed string’s is extremely slow.

Rather, I’d suggest you checkout my shaps80/MarkdownText package which enables almost full markdown spec support using only native SwiftUI so its extremely performant, animatable, etc…

It also provides a full styling API (using modifiers) so you have full control over the rendered output 👍🏻

If the text you have is in HTML, I’d recommend using some kind of converter to get it into Markdown, will honestly be far more performant than UITextView, since that uses a webview to do its rendering.

@shaps80 shaps80 closed this as completed Dec 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants