在swiftUI中目前只有textField使用,没法分行,中文输入也有问题这个我就不说了,然后没法监听输入内容,当然你可以写一个binding可是实现,网上找的分行输入不是插入输入有问题就是删除有问题。下面开始
首先新建一个文件
import SwiftUI
import UIKit
struct MyTextField: UIViewRepresentable {
func makeUIView(context: Context) -> UITextField {
}
func updateUIView(_ uiView: UITextField, context: Context) {
}
}
这是基本,然后写我们需要自定义的值
struct MyTextField: UIViewRepresentable {
let keyboardType: UIKeyboardType
@Binding var text: String
@State var placeholder : String
var paddingHor:CGFloat = 10
func makeUIView(context: Context) -> UITextField {
let textField = UITextField(frame: .zero)
textField.keyboardType = self.keyboardType
textField.delegate = context.coordinator
textField.text = self.text
textField.returnKeyType = UIReturnKeyType.done
textField.backgroundColor = .white
textField.placeholder = placeholder
textField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: paddingHor, height: 1));
textField.leftViewMode = .always
return textField
}
}
以上就已经完成uitextView,然后设置代理,和其他的优化设置
struct MyTextField: UIViewRepresentable {
let keyboardType: UIKeyboardType
@Binding var text: String
@State var placeholder : String
var paddingHor:CGFloat = 10
func makeUIView(context: Context) -> UITextField {
let textField = UITextField(frame: .zero)
textField.keyboardType = self.keyboardType
textField.delegate = context.coordinator
textField.text = self.text
textField.returnKeyType = UIReturnKeyType.done
textField.backgroundColor = .white
textField.placeholder = placeholder
textField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: paddingHor, height: 1));
textField.leftViewMode = .always
return textField
}
func updateUIView(_ uiView: UITextField, context: Context) {
DispatchQueue.main.async{
self.$text.wrappedValue = uiView.text!
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self,text: self.$text)
}
class Coordinator: NSObject, UITextFieldDelegate {
var parent: MyTextField
var text: Binding<String>
init(_ textField: MyTextField,text:Binding<String>) {
self.parent = textField
self.text = text
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
//收起键盘
textField.resignFirstResponder()
self.text.wrappedValue = textField.text!
return true
}
//结束编辑
func textFieldDidEndEditing(_ textField: UITextField) {
self.text.wrappedValue = textField.text!
print("结束")
}
//开始编辑
func textFieldDidBeginEditing(_ textField: UITextField) {
print("开始")
}
//这里是监听输入,这里的string是最新的输入数据,可实现像微信那样的@人的功能
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
self.text.wrappedValue = string
//比如这样实现@弹窗
//if string == "@"{
//self.showact.wrappedValue.toggle()
// }
return true
}
}
}
下面的是UITextView的方法
struct TextViewTyped: UIViewRepresentable {
let keyboardType: UIKeyboardType
@Binding var text: String
@Binding var endEdit:Bool
func makeUIView(context: Context) -> UITextView {
let textView = UITextView(frame: .zero)
textView.keyboardType = self.keyboardType
textView.delegate = context.coordinator
textView.inputAccessoryView = .none
textView.text = self.text
textView.textColor = .black
textView.returnKeyType = UIReturnKeyType.default
textView.backgroundColor = .white
return textView
}
func updateUIView(_ uiView: UITextView, context: Context) {
DispatchQueue.main.async {
self.$text.wrappedValue = uiView.text!
//自动弹键盘
if uiView.window != nil, !uiView.isFirstResponder {
uiView.becomeFirstResponder()
}
if self.endEdit == true {
uiView.resignFirstResponder()
}
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self,text: self.$text)
}
class Coordinator: NSObject, UITextViewDelegate {
var parent: TextViewTyped
var text: Binding<String>
init(_ textView: TextViewTyped,text:Binding<String>) {
self.parent = textView
self.text = text
}
// func textFieldShouldReturn(_ textView: UITextField) -> Bool {
// //收起键盘
// textView.resignFirstResponder()
//
// self.text.wrappedValue = textView.text!
//
//
// return true
// }
//结束编辑
func textViewDidEndEditing(_ textView: UITextView) {
textView.resignFirstResponder()
}
//开始编辑
func textViewDidBeginEditing(_ textView: UITextView) {
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
self.text.wrappedValue = text
//print(text)
return true
}
}
}
无论是哪种都需要在updateView中实现获取获取值,这样才能获取正确的值
当然这样也达不到弹不起键盘,想要实现输入发布挡住输入内容还得写其他方法