簡體   English   中英

在 macOS-SwiftUI 中禁用 TextEditor 的選擇光標

[英]Disable selection cursor for TextEditor in macOS-SwiftUI

在此處輸入圖片說明

我可以使用以下代碼禁用 TextEditor 的選擇和編輯:

TextEditor(text: self.$content)
    .allowsHitTesting(false)
    .disabled(true)

但這不會影響我的光標懸停在其中時,這就是我想要的。 有沒有辦法在禁用時將主光標保持在 TextEditor 內,而不是選擇光標?

我想要這個是因為我在 TextEditor 上方的 ZStack 中的其他視圖始終都有這個選擇光標,這很煩人。

您可以像這樣使用overlay修飾符:

struct ContentView: View {
    
    @State private var string: String = "Hello, World!"
    @State private var disableStringSelection: Bool = Bool()
    
    var body: some View {
        
        VStack(spacing: 5.0) {
            
            Color.white
                .overlay(disableStringSelection ? Text(string).font(Font.body).padding(.leading, 5.0) : nil, alignment: .topLeading)
                .overlay(disableStringSelection ? nil : TextEditor(text: $string).font(Font.body))
                .cornerRadius(10.0)

            Button(disableStringSelection ? "Enable Selection" : "Disable Selection") { disableStringSelection.toggle() }
            
        }
        .padding(5.0)

    }
    
}

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM