Swiftui Texteditor禁用返回键

发布于 2025-01-28 05:58:27 字数 488 浏览 2 评论 0原文

我想知道如何在Swiftui中的键盘中禁用“返回”键?

我需要一个支持多行(约140个字符)的文本字段,因此我发现可以使用Texteditor。问题在于我找不到如何将板上的“返回”键更改为“完成”或简单地将其禁用。

有人知道该怎么做吗?

这是一些代码:

@State var annotationText = "Say something..."
//
TextEditor(text: $annotationText)
    .onReceive(annotationText.publisher.collect()) {
      annotationText = String($0.prefix(140))
  }
 .onTapGesture {
     if annotationText == "Say something..." {
         annotationText = ""
     }
 }
.frame(height: 50)

I was wondering how I can disable the "return" key in the keyboard for TextEditor in SwiftUI?

I need a TextField that supports multiple lines (around 140 characters) so I found that I can use TextEditor. The problem is that I can't find out how to change the "return" key on the board to "Done" or simply disable it.

Does anyone know how to do this?

Here's some code:

@State var annotationText = "Say something..."
//
TextEditor(text: $annotationText)
    .onReceive(annotationText.publisher.collect()) {
      annotationText = String($0.prefix(140))
  }
 .onTapGesture {
     if annotationText == "Say something..." {
         annotationText = ""
     }
 }
.frame(height: 50)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰火雁神 2025-02-04 05:58:27

通过手动获取/设置绑定变量找到了此解决方案:

TextEditor(text: Binding(
    get: {
        return annotationText
    },
    set: { value in
        var newValue = value
        if value.contains("\n") {
            newValue = value.replacingOccurrences(of: "\n", with: "")
        }
        annotationText = newValue
    }
))

Found this solution by manually getting/setting the binding variable:

TextEditor(text: Binding(
    get: {
        return annotationText
    },
    set: { value in
        var newValue = value
        if value.contains("\n") {
            newValue = value.replacingOccurrences(of: "\n", with: "")
        }
        annotationText = newValue
    }
))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文