我正在使用UITextField,因为我想要一个自定义弹出式键盘。但是,我不希望用户能够更改插入点或访问复制粘贴菜单。
我找到了两个有用的stackoverflow问题,并尝试实现它们:
我已经通过继承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可用,所以如何判断它被点击的频率。
答案 0 :(得分:2)
我发现这篇有用的文章为我解决了这个问题:https://www.transpire.com/insights/blog/disabling-caret-text-entry-uitextfields/
您需要在UITextField
子类中添加以下替代(它们基本上禁用了UIMenuController
并隐藏选择和插入符号):
- (CGRect)caretRectForPosition:(UITextPosition*) position {
return CGRectZero;
}
- (NSArray *)selectionRectsForRange:(UITextRange *)range {
return nil;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
return NO;
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
false
}
override func selectionRects(for range: UITextRange) -> [UITextSelectionRect] {
[]
}
override func caretRect(for position: UITextPosition) -> CGRect {
.zero
}
答案 1 :(得分:0)
UITextView有一个可以帮助你的属性selectable