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
相关产品推荐
相关产品推荐

