ios 如何禁用UITextField的选择?

rsaldnfx  于 2023-05-19  发布在  iOS
关注(0)|答案(2)|浏览(214)

我使用UITextField是因为我想有一个自定义弹出键盘。但是,我不希望用户能够更改插入点或访问复制、粘贴菜单。
我发现了两个有用的stackoverflow问题,并尝试实现它们:

(iPhone)如何处理UITextView上的触摸?
我已经通过子类化UITextField并实现方法删除了菜单:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    return NO;
}

但是,当用户双击该字段时,我未能阻止该字段被选中:
我已经尝试删除我认为对选择行为负责的gestureRecognizers,但没有成功。我做错了什么?

@property (nonatomic, strong) MinimalTextField *inputText;
...
@synthesize inputText;
...

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear: animated];    
    NSLog(@"%ld gestureRecognizers initially ", (long)inputText.gestureRecognizers.count);

    for (UIGestureRecognizer *gestureRecognizer in inputText.gestureRecognizers) {        
        if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
            UITapGestureRecognizer *tapGestureRecognizer = (UITapGestureRecognizer *)gestureRecognizer;
            if ([tapGestureRecognizer numberOfTapsRequired] == 2) {
                NSLog(@"found & removed: %@", tapGestureRecognizer);
                [inputText removeGestureRecognizer:tapGestureRecognizer];
            }
        }
        if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
            UILongPressGestureRecognizer *longPressGestureRecognizer = (UILongPressGestureRecognizer *)gestureRecognizer;
            NSLog(@"found & removed: %@", longPressGestureRecognizer);
            [inputText removeGestureRecognizer:longPressGestureRecognizer];
        }
    }

    NSLog(@"%ld gestureRecognizers remaining", (long)inputText.gestureRecognizers.count);

    for (UIGestureRecognizer *gestureRecognizer in inputText.gestureRecognizers) {
        NSLog(@"gestureRecognizer: %@", gestureRecognizer);
    }
}

这段代码会产生以下输出,所以我知道它在工作,但它无法影响双击动作。

7 gestureRecognizers initially 
found & removed: <UITextTapRecognizer: 0x7ff6086571f0; state = Possible; delaysTouchesEnded = NO; view = <MinimalTextField 0x7ff608414b10>; target= <(action=oneFingerDoubleTap:, target=<UITextInteractionAssistant 0x7ff608652de0>)>; numberOfTapsRequired = 2>
found & removed: <UILongPressGestureRecognizer: 0x7ff608658180; state = Possible; delaysTouchesEnded = NO; view = <MinimalTextField 0x7ff608414b10>; target= <(action=twoFingerRangedSelectGesture:, target=<UITextInteractionAssistant 0x7ff608652de0>)>>
found & removed: <UIVariableDelayLoupeGesture: 0x7ff608658a40; state = Possible; delaysTouchesEnded = NO; view = <MinimalTextField 0x7ff608414b10>; target= <(action=loupeGesture:, target=<UITextInteractionAssistant 0x7ff608652de0>)>>

4 gestureRecognizers remaining
gestureRecognizer: <UITextTapRecognizer: 0x7ff608653960; state = Possible; delaysTouchesEnded = NO; view = <MinimalTextField 0x7ff608414b10>; target= <(action=oneFingerTripleTap:, target=<UITextInteractionAssistant 0x7ff608652de0>)>; numberOfTapsRequired = 3>
gestureRecognizer: <UITextTapRecognizer: 0x7ff6086576e0; state = Possible; delaysTouchesEnded = NO; view = <MinimalTextField 0x7ff608414b10>; target= <(action=twoFingerSingleTap:, target=<UITextInteractionAssistant 0x7ff608652de0>)>; numberOfTouchesRequired = 2>
gestureRecognizer: <UITapAndAHalfRecognizer: 0x7ff608657c70; state = Possible; view = <MinimalTextField 0x7ff608414b10>; target= <(action=tapAndAHalf:, target=<UITextInteractionAssistant 0x7ff608652de0>)>>
gestureRecognizer: <UITextTapRecognizer: 0x7ff6086585f0; state = Possible; delaysTouchesEnded = NO; view = <MinimalTextField 0x7ff608414b10>; target= <(action=oneFingerTap:, target=<UITextInteractionAssistant 0x7ff608652de0>)>>

我甚至尝试将以下代码添加到UITextField的子类中:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    NSLog(@"Gesture should begin");
    if ([gestureRecognizer isKindOfClass:[UIRotationGestureRecognizer class]])
        NSLog(@"rotate");
    if ([gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]])
        NSLog(@"pinch");
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
        NSLog(@"tap");
        NSLog(@"numberOfTouches: %ld", (long)gestureRecognizer.numberOfTouches);
    }
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
        NSLog(@"pan");
    if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
        NSLog(@"long");
    if ([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]])
        NSLog(@"swipe");
    return YES;
}

但是,没有可用的属性gestureRecognizer.numberOfTaps,所以我如何知道它被点击的频率呢?

v1l68za4

v1l68za41#

你需要在你的UITextField子类中添加以下覆盖(它们基本上是禁用UIMenuController并隐藏选择和插入字符):

Objective-C

- (CGRect)caretRectForPosition:(UITextPosition*) position {
    return CGRectNull;
}

- (NSArray *)selectionRectsForRange:(UITextRange *)range {
    return nil;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    return NO;
}

Swift

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    false
}

override func selectionRects(for range: UITextRange) -> [UITextSelectionRect] {
    []
}

override func caretRect(for position: UITextPosition) -> CGRect {
    .null
}
mf98qq94

mf98qq942#

UITextView有一个可选属性,可以帮助您

相关问题