我的文本编辑器有问题,在文本编辑器上编辑文本后无法隐藏键盘。
@State var monTexte: String = "
var body: some View {
VStack {
Spacer()
.frame(height :15)
.clipped()
Text("Project ")
.font(Font.system(size: 39.00))
.fontWeight(.black)
.foregroundColor(Color.white)
.multilineTextAlignment(.center)
.padding(.all, 16.0)
.clipped()
TextEditor(text: $monTexte)
.keyboardType(.alphabet)
.font(.subheadline)
.padding(.horizontal)
.font(Font.system(size: 38.00))
.frame(minWidth: 10, maxWidth: .infinity, minHeight: 10, maxHeight: 200, alignment: .topLeading)
.border(Color.black)
.clipped()
}
}
}
我找到了一种使用文本字段隐藏键盘但不使用文本编辑器的方法 你可以帮帮我吗
谢谢“Asperi”,使用下面的代码可以正常工作:
var body: some View {
VStack {
Spacer()
.frame(height :15)
.clipped()
Text("Project ")
.font(Font.system(size: 39.00))
.fontWeight(.black)
.foregroundColor(Color.white)
.multilineTextAlignment(.center)
.padding(.all, 16.0)
.clipped()
HStack {
Spacer()
Button("Close Keyboard") {
UIApplication.shared.endEditing()
}.font(Font.system(size: 20))
.foregroundColor(Color.blue)
}.clipped()
TextEditor(text: $monTexte)
.keyboardType(.alphabet)
.font(.subheadline)
.padding(.horizontal)
.font(Font.system(size: 38.00))
.frame(minWidth: 10, maxWidth: .infinity, minHeight: 10, maxHeight: 200, alignment: .topLeading)
.border(Color.black)
.clipped()
}
}
}
//----------------------------------------------------//
// Masquer le clavier
//----------------------------------------------------//
extension UIApplication {
func endEditing() {
sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
}```
我发现这对我来说足够有效。它是纯粹的 SwiftUI,无需求助于 UIKit。
struct TestView: View {
@State private var response = ""
@FocusState private var responseIsFocussed: Bool // dismiss response editor keyboard when hit Return
var body: some View {
TextEditor(text: $response)
.focused($responseIsFocussed)
.onReceive(response.publisher.last()) {
if ($0 as Character).asciiValue == 10 { // ASCII 10 = newline
responseIsFocussed = false // unfocus TextEditor to dismiss keyboard
response.removeLast() // remove newline at end to prevent retriggering...
}
}
}
}
视图触发器上的
.focused()
修饰符将触发器附加到文本编辑器,使您能够以编程方式聚焦(显示)或取消聚焦(关闭)键盘。 .onReceive()
捕获按键输入;它检查字符串中输入的最后一个字符是否是换行符(ASCII 10),如果是,则触发 un焦点。立即从字符串末尾删除此 Return/newline 很重要,否则它会继续触发...
警告:这只会触发 last 字符;如果您编辑字符串并中途移动光标并按回车键,则不会发生任何事情(实际上,在我看来,这有点直观......)
我更喜欢使用工具栏。这样你就不会破坏换行逻辑。 假设你有 2 个文本编辑器:
VStack {
TextEditor(text: $subjectText)
.focused($focusField, equals: .subject)
TextEditor(text: $messageText)
.focused($focusField, equals: .message)
}
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Spacer()
Button(focusField == .subject ? "Next" : "Done") {
if focusField == .subject {
focusField = .message
} else {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
focusField = nil
}
}
}
}
哪里
@FocusState private var focusField: Field?
enum Field: Hashable {
case subject
case message
}
像 protspace 一样,我更喜欢使用允许用户在文本编辑器中创建新行的解决方案。我发现 designcode.io 的这个解决方案非常简单:
extension View {
func hideKeyboard() {
let resign = #selector(UIResponder.resignFirstResponder)
UIApplication.shared.sendAction(resign, to: nil, from: nil, for: nil)
}
}
然后将其附加到您视图的相关部分:
.onTapGesture {
hideKeyboard()
}