简体   繁体   中英

use UIView package in SwiftUI

RichTextView is a UIView package. It cannot be used as following:

import SwiftUI
import RichTextView

struct ContentView1: View {

    var body: some View {
        VStack {
            RichTextView(              // <-- Wrong!
                input: "Test",
                latexParser: LatexParser(),
                font: UIFont.systemFont(ofSize: UIFont.systemFontSize),
                textColor: UIColor.black,
                isSelectable: true,
                isEditable: false,
                latexTextBaselineOffset: 0,
                interactiveTextColor: UIColor.blue,
                textViewDelegate: nil,
                frame: CGRect.zero,
                completion: nil
            )
        }
    }
}

I also find the reference How to wrap a custom UIView for SwiftUI as following:

struct TextView: UIViewRepresentable {
    @Binding var text: String

    func makeUIView(context: Context) -> UITextView {
        return UITextView()
    }

    func updateUIView(_ uiView: UITextView, context: Context) {
        uiView.text = text
    }
}

struct ContentView : View {
    @State var text = ""

    var body: some View {
        TextView(text: $text)
            .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
    }
}

But I try many methods to use RichTextView with UIViewRepresentable . It still not works. As following:

struct RichTextView: UIViewRepresentable {
    @Binding var text: String

    let richTextView = RichTextView(
        input: text, <-- Cannot use instance member 'text' within property initializer; property initializers run before 'self' is available
        latexParser: LatexParser(),
        font: UIFont.systemFont(ofSize: UIFont.systemFontSize),
        textColor: UIColor.black,
        isSelectable: true,
        isEditable: false,
        latexTextBaselineOffset: 0,
        interactiveTextColor: UIColor.blue,
        textViewDelegate: nil,
        frame: CGRect.zero,
        completion: nil
    )

    func makeUIView(context: Context) -> UITextView {

        return UITextView()
    }

    func updateUIView(_ uiView: UITextView, context: Context) {
        uiView.text = richTextView  <--- changed
    }
}

Updated: The following still does not work.

struct RichTextView: UIViewRepresentable {             <-- Error:Type 'RichTextView' does not conform to protocol 'UIViewRepresentable'

  @Binding var text: String

  func makeUIView(context: Context) -> RichTextView {
    return RichTextView(                               <-- Error:Extra arguments at positions #1, #2, #3, #4, #5, #6, #7, #8, #9, #10, #11 in call
      input: text,                                     <-- Error:Missing argument for parameter 'text' in call
      latexParser: LatexParser(),
      font: UIFont.systemFont(ofSize: UIFont.systemFontSize),
      textColor: UIColor.black,
      isSelectable: true,
      isEditable: false,
      latexTextBaselineOffset: 0,
      interactiveTextColor: UIColor.blue,
      textViewDelegate: nil,
      frame: CGRect.zero,
      completion: nil
    )
  }

  func updateUIView(_ richTextView: RichTextView, context: Context) {
    richTextView.text = text
  }
}

How to make it? Thanks for any help.

I think you're looking for something more along these lines.

struct RichTextView: UIViewRepresentable {

  @Binding var text: String

  func makeUIView(context: Context) -> RichTextView {
    return RichTextView(
      input: text,
      latexParser: LatexParser(),
      font: UIFont.systemFont(ofSize: UIFont.systemFontSize),
      textColor: UIColor.black,
      isSelectable: true,
      isEditable: false,
      latexTextBaselineOffset: 0,
      interactiveTextColor: UIColor.blue,
      textViewDelegate: nil,
      frame: CGRect.zero,
      completion: nil
    )
  }

  func updateUIView(_ richTextView: RichTextView, context: Context) {
    richTextView.text = text
  }
}

UIViewRepresentable makes use of swift generics meaning that you can specify the type of view in question, in this scenario you just return the RichTextView .

I haven't tested this so it may need some tweaking but hopefully it points you in the correct direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM