You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift中如何禁用SkyFloatingLabelTextField的复制粘贴功能?

禁用SkyFloatingLabelTextField复制粘贴功能的实现方法

SkyFloatingLabelTextField继承自UITextField,可通过重写canPerformAction:withSender:方法禁用指定的交互操作,具体实现如下:

  • 自定义子类继承SkyFloatingLabelTextField(适合需要复用的场景,逻辑清晰无全局污染)
    Swift实现代码:

    class CustomFloatingTextField: SkyFloatingLabelTextField {
        override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
            // 按需添加需要禁用的操作
            let disabledActions = [
                #selector(UIResponderStandardEditActions.copy(_:)),
                #selector(UIResponderStandardEditActions.paste(_:)),
                #selector(UIResponderStandardEditActions.cut(_:)),
                #selector(UIResponderStandardEditActions.select(_:)),
                #selector(UIResponderStandardEditActions.selectAll(_:))
            ]
            if disabledActions.contains(action) {
                return false
            }
            return super.canPerformAction(action, withSender: sender)
        }
    }
    

    Objective-C实现代码:

    // CustomFloatingTextField.h 文件
    @interface CustomFloatingTextField : SkyFloatingLabelTextField
    @end
    
    // CustomFloatingTextField.m 文件
    @implementation CustomFloatingTextField
    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
        NSArray *disabledActions = @[
            NSStringFromSelector(@selector(copy:)),
            NSStringFromSelector(@selector(paste:)),
            NSStringFromSelector(@selector(cut:)),
            NSStringFromSelector(@selector(select:)),
            NSStringFromSelector(@selector(selectAll:))
        ];
        if ([disabledActions containsObject:NSStringFromSelector(action)]) {
            return NO;
        }
        return [super canPerformAction:action withSender:sender];
    }
    @end
    
  • 后续使用时,直接替换原有SkyFloatingLabelTextField的类为自定义的CustomFloatingTextField即可生效。如果仅需要禁用部分操作,调整disabledActions数组中的选择器条目即可。

内容的提问来源于stack exchange,提问作者Manish Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 10:36:05