keitaoouchi / MarkdownView

Markdown View for iOS.
MIT License
1.97k stars 206 forks source link

How to reload new content in SwiftUI? #101

Open wyk111wyk opened 1 year ago

vuvdv3101 commented 5 months ago

Can try this. Hope resolve your issue

public struct MarkdownUI2: UIViewRepresentable {
  private let markdownView: MarkdownView
  @Binding public var body: String

  public init(body: Binding<String>, css: String? = nil, plugins: [String]? = nil, stylesheets: [URL]? = nil, styled: Bool = true) {
    self._body = body
    self.markdownView = MarkdownView(css: css, plugins: [], stylesheets: stylesheets, styled: styled)
    self.markdownView.isScrollEnabled = false
  }

  public func onTouchLink(perform action: @escaping ((URLRequest) -> Bool)) -> MarkdownUI2 {
    self.markdownView.onTouchLink = action
    return self
  }

  public func onRendered(perform action: @escaping ((CGFloat) -> Void)) -> MarkdownUI2 {
    self.markdownView.onRendered = action
    return self
  }
}

extension MarkdownUI2 {

  public func makeUIView(context: Context) -> MarkdownView {
    return markdownView
  }

  public func updateUIView(_ uiView: MarkdownView, context: Context) {
      self.markdownView.load(markdown: self.body)
  }
}