我正在使用UIViewRepresentable
包装器来创建可绑定的多行UITextVIew
,因为对本地SwiftUI TextField
尚无 still 支持。只要在UITextView上设置了isScrollEnabled = true
,一切都可以正常工作。
有没有一种方法可以使文本自动换行而不使UITextView
滚动?
import SwiftUI
struct TextfieldIssue: View {
@State var text: String = "Hello, I am some long text that will wrap if isScrollEnabled is set to true, but not if isScrollEnabled is set to false"
var body: some View {
MultilineTextField(text: $text)
}
}
struct MultilineTextField: UIViewRepresentable {
private let view = UITextView()
@Binding var text: String
func makeUIView(context: UIViewRepresentableContext<MultilineTextField>) -> UITextView {
view.isEditable = true
view.font = UIFont.systemFont(ofSize: 24)
// Text wraps fine as long as this is true
view.isScrollEnabled = true
// Text does not wrap if this is uncommented
// view.isScrollEnabled = false
view.text = text
view.delegate = context.coordinator
return view
}
func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<MultilineTextField>) {
uiView.text = text
}
func makeCoordinator() -> TextFieldCoordinator {
let coordinator = TextFieldCoordinator(self)
self.view.delegate = coordinator
return coordinator
}
}
class TextFieldCoordinator : NSObject, UITextViewDelegate {
var parent: MultilineTextField
init(_ multilineTextField: MultilineTextField) {
self.parent = multilineTextField
}
func textViewDidChange(_ textView: UITextView) {
self.parent.text = textView.text
}
}
struct TextfieldIssue_Previews: PreviewProvider {
static var previews: some View {
TextfieldIssue()
}
}
答案 0 :(得分:6)
只需将其添加到axios.request({
method: "POST",
url: `${PRIVATE_API_ROOT}/materials`,
data: formData,
headers: { Authorization: `Bearer ${token}` },
onUploadProgress: ProgressEvent => {
this.setState({
progressStatus: parseFloat(ProgressEvent.loaded / ProgressEvent.total * 100).toFixed(2),
})
}
}).then(data => {
this.setState({
progressStatus: 100,
loading: false,
error: ''
})
}).catch(function (error) {
//handle error
this.setState({ error: error.message })
});
```
,文本就会自动换行。
makeUIView
这是完整更新的view.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
函数:
makeUIView