ios – Messages alike UiTextView in SwiftUI


Im making an attempt to make messages alike TextField which is one line tall and its most peak is 5 traces. When person reaches its most peak (5 traces), scrolling can be enabled and TextField will cease its peak enlargement.

I’ve this piece of code, which is doing peak enlargement on every newline, however doesn’t cease, when person reaches 5 traces peak and doesn’t allow scrolling:

struct AutoAdjustingTextView: UIViewRepresentable {
@Binding var textual content: String

func makeUIView(context: Context) -> UITextView {
    let textView = UITextView()
    textView.isScrollEnabled = false
    textView.font = UIFont.systemFont(ofSize: 17)
    textView.delegate = context.coordinator
    textView.textContainerInset = .zero
    textView.textContainer.lineFragmentPadding = 0
    textView.setContentHuggingPriority(.defaultHigh, for: .vertical)
    return textView
}

func updateUIView(_ uiView: UITextView, context: Context) {
    uiView.textual content = textual content
    updateTextViewHeight(uiView)
}

non-public func updateTextViewHeight(_ textView: UITextView) {
    let measurement = textView.sizeThatFits(CGSize(width: textView.body.width, peak: .greatestFiniteMagnitude))
    if textView.body.measurement.peak != measurement.peak {
        textView.body.measurement.peak = measurement.peak
    }
}

func makeCoordinator() -> Coordinator {
    Coordinator(self)
}

class Coordinator: NSObject, UITextViewDelegate {
    var mum or dad: AutoAdjustingTextView

    init(_ mum or dad: AutoAdjustingTextView) {
        self.mum or dad = mum or dad
    }

    func textViewDidChange(_ textView: UITextView) {
        mum or dad.textual content = textView.textual content
        mum or dad.updateTextViewHeight(textView)
    }
}

}

Im operating on IOS 14

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles