如何解决TextView展开父视图,导致文本被隐藏
我的问题显示在下面的照片中:
我尝试在父视图和文本字段上使用.fixedSize(horizontal:vertical),但没有任何积极结果。
TheTextField:
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()
解决方法
您可以轻松地从我们项目的.storyboard文件中编辑标签属性。 将行更改为0,并将换行符属性更改为所需的类型,然后可以根据需要设置任意多行。Check this image to see what I'm saying
,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的建议根据文本的长度动态更改高度。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。