如何防止在UITextField
隐藏光标/插入符号/放大镜的同时编辑文本,但仍然显示键盘,因为我使用的inputView
是UIPickerView
/ UIDatePickerView
?
设置userInteractionEnabled
为NO
不起作用,因为它不再接收任何触摸并且不会显示键盘。
如何防止在UITextField
隐藏光标/插入符号/放大镜的同时编辑文本,但仍然显示键盘,因为我使用的inputView
是UIPickerView
/ UIDatePickerView
?
设置userInteractionEnabled
为NO
不起作用,因为它不再接收任何触摸并且不会显示键盘。
子类 UITextField
//Disables caret
- (CGRect)caretRectForPosition:(UITextPosition *)position
{
return CGRectZero;
}
//Disables magnifying glass
-(void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
{
gestureRecognizer.enabled = NO;
}
[super addGestureRecognizer:gestureRecognizer];
}
在你的 UITextFieldDelegate
//Prevent text from being copied and pasted or edited with bluetooth keyboard.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return NO;
}
现在只需根据 UIPickerView/UIDatePicker 的结果以编程方式设置文本。
在 iOS 7 中隐藏光标要简单得多。仍然需要一些技巧来禁用放大镜
textField.tintColor = [UIColor clearColor];
我希望这对你有帮助。
设置光标 UIColor -> 空。在 UI 中,它将被隐藏。
[[self.textField valueForKey:@"textInputTraits"] setValue:[UIColor clearColor] forKey:@"insertionPointColor"];
我发现最好的解决方案是
- (CGRect) caretRectForPosition:(UITextPosition*) position
{
return CGRectZero;
}
- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
return nil;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(copy:) || action == @selector(selectAll:) || action == @selector(paste:))
{
returnNO;
}
return [super canPerformAction:action withSender:sender];
}
http://b2cloud.com.au/tutorial/disabling-the-caret-and-text-entry-in-uitextfields/
没有UITextField
交互,但仍然使用inputView
:
使用这些方法子类 UITextField:
// Hide the cursor
- (CGRect)caretRectForPosition:(UITextPosition*)position
{
return CGRectZero;
}
// All touches inside will be ignored
// and intercepted by the superview
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
return NO;
}
最后一种方法将单独阻止任何编辑和放大镜,因为您将无法点击UITextField
.
例如,如果您在 a 中使用文本字段,这非常UITableViewCell
有用,然后可以通过 切换 firstResponder 状态tableView:didSelectRowAtIndexPath:
。
要禁用与文本字段的任何交互,除了使其成为第一响应者之外,您只需在文本字段上放置一个相同大小的 UIButton。按钮点击事件的代码可能是这样的:
- (IBAction)btnEditPhoneTapped:(id)sender
{
if (self.tfClientPhoneNo.isFirstResponder == NO) [self.tfClientPhoneNo becomeFirstResponder];
}