在 swift 上,如何隐藏光标并以编程方式为 UITextfield 禁用 cut/paste

On swift, how to hide cursor and disable cut/paste for UITextfield programmatically

我在创建 UITexfield 的子 class 以隐藏 copy/past 等按钮时遇到问题,当我单击 TextField 时。这是 class:

的代码
class UITextFieldCustom: UITextField {

    var enableLongPressActions = false
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!}
    override init(frame: CGRect) {
        super.init(frame: frame)}
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
            return enableLongPressActions
    }

}

当我将它用于情节提要中的 texfield 时,代码运行良好,但是当以编程方式制作 textField 时,我收到消息:Thread 1: signal SIGABRT

这是我的 textField 在我的 View Controller 扩展中的部分代码中的声明:

extension MenuViewController {
    
    func tabBarCentre(nameTextField: UITextFieldCustom) {

        let titleLeftbutton = UIButton(type: .system)
        titleLeftbutton.tintColor = #colorLiteral(red: 0.370555222, green: 0.3705646992, blue: 0.3705595732, alpha: 1)
        let leftButtonConfig = UIImage.SymbolConfiguration(
            pointSize: 20,
            weight: .bold,
            scale: .small)
        let leftButtonImage = UIImage(
                   systemName: "chevron.left",
                   withConfiguration: leftButtonConfig)
            titleLeftbutton.setImage(leftButtonImage, for: .normal)
        titleLeftbutton.addTarget(self, action: #selector(dateMoinsUn), for: .touchUpInside)
        
        
        // Création du text central qui recevra le DatePicker
        nameTextField.frame = CGRect(x: 0, y: 0, width: 70, height: 25)
        nameTextField.backgroundColor = #colorLiteral(red: 0.9685223699, green: 0.9686879516, blue: 0.9685119987, alpha: 1)
        nameTextField.placeholder = "Ajouter date"
        nameTextField.font = nameTextField.font?.withSize(15)
        nameTextField.font = UIFont.preferredFont(forTextStyle: .headline)
        nameTextField.textAlignment = .center
        
        

错误信息在我的 viewController :

class MenuViewController: UIViewController {
override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        tabBarCentre(nameTextField: titleNavBarTextField) // THE ERROR IS HERE !!!!
        self.titleNavBarTextField.setInputViewDatePicker(target: self, textField: titleNavBarTextField, selector1: #selector(tapped), selector2: #selector(todayPressed))
        titleNavBarTextField.text = displayToday() 
        }
}

谢谢。

将此方法添加到您的代码中并查看更改。您还可以自定义您可以使用文本字段执行的操作。如果您希望禁用所有文本字段操作,那么只需在 func canPerformAction 方法中使用“return false”即可。

override func caretRect(for position: UITextPosition) -> CGRect {
    return CGRect.zero
}
   
override func selectionRects(for range: UITextRange) -> [UITextSelectionRect] {
    return []
}
   
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    if action == #selector(UIResponderStandardEditActions.cut(_:)) || action == #selector(UIResponderStandardEditActions.selectAll(_:)) || action == #selector(UIResponderStandardEditActions.paste(_:)) {
        return false
    }
    // Default
    return super.canPerformAction(action, withSender: sender)**