gpt4 book ai didi

ios - 我们可以在 SwiftUI 中禁用 Textfield 的复制/粘贴选项吗?

转载 作者:行者123 更新时间:2023-12-04 11:36:27 29 4
gpt4 key购买 nike

我想在 SwiftUI 中禁用从我的文本字段中复制/粘贴的选项。如何做到这一点?

最佳答案

使用 UIViewRepresentable 类制作这样的包装类。

import SwiftUI

struct CustomeTextField: View {

@State var textStr = ""


var body: some View {
VStack(spacing: 10) {
Text("This is textfield:")
.font(.body)
.foregroundColor(.gray)

TextFieldWrapperView(text: self.$textStr)
.background(Color.gray)
.frame(width: 200, height: 50)
}
.frame(height: 40)
}
}


struct TextFieldWrapperView: UIViewRepresentable {

@Binding var text: String

func makeCoordinator() -> TFCoordinator {
TFCoordinator(self)
}
}

extension TextFieldWrapperView {


func makeUIView(context: UIViewRepresentableContext<TextFieldWrapperView>) -> UITextField {
let textField = UITextField()
textField.delegate = context.coordinator
return textField
}


func updateUIView(_ uiView: UITextField, context: Context) {

}
}

class TFCoordinator: NSObject, UITextFieldDelegate {
var parent: TextFieldWrapperView

init(_ textField: TextFieldWrapperView) {
self.parent = textField
}

func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
if action == #selector(UIResponderStandardEditActions.paste(_:)) {
return false
}
return canPerformAction(action: action, withSender: sender)
}
}

关于ios - 我们可以在 SwiftUI 中禁用 Textfield 的复制/粘贴选项吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60148518/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com