【问题标题】:UITextView dynamically change height based on content?UITextView 根据内容动态改变高度?
【发布时间】:2020-06-06 12:57:54
【问题描述】:

我被这个问题困扰了一段时间,所以我希望能得到一些帮助。我有一个使用 UITextView 的 SwiftUI 应用程序,但是当我将内容放入其中时,文本不会换行。这个 SwiftUI 视图我正在显示和编辑文本,但它目前都不适用。

第二个框中的文字更长,但高度没有变化。

func makeUIView(context: Context) -> UITextView {
        view.isScrollEnabled = false
        view.isEditable = editable
        view.isUserInteractionEnabled = true
        view.contentInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
        view.text = self.text

        // For debugging
        view.layer.borderColor = UIColor.black.cgColor
        view.layer.borderWidth = 1.0

        var fontPref = UIFont.preferredFont(forTextStyle: render.font)
        if render.bold && render.italic {
            fontPref = fontPref.bolditalic()
        } else if render.bold {
            fontPref = fontPref.bold()
        } else if render.italic {
            fontPref = fontPref.italic()
        }

        view.font = fontPref
        view.textColor = render.color
        view.textAlignment = render.align
        view.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        view.textContainer.lineBreakMode = .byWordWrapping

        view.delegate = context.coordinator

        return view
    }

我尝试了一些解决方案,但没有一个适合我的用例。

谢谢!

【问题讨论】:

    标签: swift uikit swiftui uitextview


    【解决方案1】:

    这是一个可以工作的自定义文本视图,可以包裹文本并调整高度:

    
    import SwiftUI
    
    struct NoteTextView: UIViewRepresentable {
      func makeCoordinator() -> Coordinator {
        Coordinator(self)
      }
    
      @Binding var text: String
      var onEndEditing: () -> Void
    
      typealias UIViewType = UITextView
      var configuration = { (view: UIViewType) in }
    
      func makeUIView(context: UIViewRepresentableContext<Self>) -> UIViewType {
        let textField = UIViewType()
        textField.delegate = context.coordinator
        textField.font = UIFont.preferredFont(forTextStyle: .body)
        textField.text = text
        textField.isScrollEnabled = true
        textField.isEditable = true
        textField.isUserInteractionEnabled = true
    
        return textField
      }
    
      func updateUIView(_ uiView: UIViewType, context: UIViewRepresentableContext<Self>) {
        uiView.text = text
        configuration(uiView)
      }
    
      class Coordinator : NSObject, UITextViewDelegate {
    
        var parent: NoteTextView
    
        init(_ uiTextView: NoteTextView) {
          self.parent = uiTextView
        }
    
        func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
          return true
        }
    
        func textViewDidChange(_ textView: UITextView) {
          self.parent.text = textView.text
        }
      }
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 2014-05-19
      相关资源
      最近更新 更多