【问题标题】:TextView expands parent view causing text to be hiddenTextView 展开父视图导致文本被隐藏
【发布时间】:2020-08-28 17:53:11
【问题描述】:

我的问题如下图所示:

我尝试在父视图和文本字段上使用 .fixedSize(horizo​​ntal:vertical) 并没有积极的结果。

文本域:

struct TheTextField: UIViewRepresentable {
    @Binding var text : String
    @Binding var placeholder : String
    
    func makeCoordinator() -> TheTextField.Coordinator {
        return TheTextField.Coordinator(parent1: self)
    }
    
    func makeUIView(context: UIViewRepresentableContext<TheTextField>) -> UITextView {
        let tview = UITextView()
        tview.isEditable = true
        tview.isUserInteractionEnabled = true
        tview.isScrollEnabled = false
        tview.text = placeholder
        tview.textColor = .gray
        tview.font = .systemFont(ofSize: 20)
        tview.delegate = context.coordinator
        return tview
    }
    
    func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<TheTextField>) {
        
    }
    
    class Coordinator : NSObject, UITextViewDelegate {
        var parent : TheTextField
        
        init(parent1 : TheTextField) {
            parent = parent1
        }
        
        func textViewDidChange(_ textView: UITextView) {
            self.parent.text = textView.text
        }
        
        func textViewDidBeginEditing(_ textView: UITextView) {
            textView.text = ""
            textView.textColor = .label
        }
    }
}

我希望 textview 不展开并将光标移动到下一行,而不是弄乱父视图。

例子:

Divider()
TheTextField(text: self.$imageToUpload.textCaption, placeholder: self.$placeholder).padding(.horizontal)
Spacer()

【问题讨论】:

  • 谢谢!我采用我的代码来实现一个动态高度,该高度将根据文本长度而变化。现在,我每次输入内容时都会遇到背景视图消失和重新出现的问题

标签: swift swiftui


【解决方案1】:
import SwiftUI

struct UITextViewWrapper: UIViewRepresentable {
    @Binding var text : String
    @Binding var calculatedHeight: CGFloat
    
    func makeCoordinator() -> UITextViewWrapper.Coordinator {
        return UITextViewWrapper.Coordinator(text: $text, height: $calculatedHeight)
    }
    
    func makeUIView(context: UIViewRepresentableContext<UITextViewWrapper>) -> UITextView {
        let tview = UITextView()
        tview.isEditable = true
        tview.isUserInteractionEnabled = true
        tview.isScrollEnabled = false
        tview.textColor = .gray
        tview.font = .systemFont(ofSize: 20)
        tview.delegate = context.coordinator
        tview.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        return tview
    }
    
    func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<UITextViewWrapper>) {
        if uiView.text != self.text {
            uiView.text = self.text
        }
        if uiView.window != nil, uiView.isFirstResponder {
            uiView.becomeFirstResponder()
        }
    }
    
    static func recalculateHeight(view: UIView, result: Binding<CGFloat>){
        let newSize = view.sizeThatFits(CGSize(width: view.frame.size.width, height: CGFloat.greatestFiniteMagnitude))
        if result.wrappedValue != newSize.height {
            DispatchQueue.main.async {
                result.wrappedValue = newSize.height // !! must be called asynchronously
            }
        }
    }
    
    class Coordinator : NSObject, UITextViewDelegate {
        var text : Binding<String>
        var calculatedHeight : Binding<CGFloat>
        
        init(text: Binding<String>, height: Binding<CGFloat>) {
            self.text = text
            self.calculatedHeight = height
        }
        
        func textViewDidChange(_ textView: UITextView) {
            text.wrappedValue = textView.text
            UITextViewWrapper.recalculateHeight(view: textView, result: calculatedHeight)
        }
        
        func textViewDidBeginEditing(_ textView: UITextView) {
            textView.text = ""
            textView.textColor = .label
        }
    }
}

我添加了根据 Asperi 建议的文本长度动态更改高度的功能。

【讨论】:

    【解决方案2】:

    您可以轻松地从我们项目的 .storyboard 文件中编辑标签属性。 将行更改为 0 并将换行符属性更改为您想要的类型,然后您可以拥有任意数量的行。Check this image to see what I'm saying

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多