Reputation: 811
I am building an application with a custom interface for entering text, and was using a UILabel for display. It then became necessary for the user to be able to copy and paste the text, so I went with a UITextField.
The problem I have is that I can't figure out how to get the UITextField to allow selection, copy, paste, etc, but have no keyboard displayed when the field is selected.
I have read numerous posts similar to this but none ask exactly the same thing, nor do any of the solutions work for me. I've tried making the field non-editable, which does block the keyboard from coming up, but also doesn't allow copying and pasting. I've also read comments suggesting subclassing the UITextField, but none I've read have given concrete examples.
Can what I want be done simply, or do I need to go the route of creating a custom view for the keyboard, or other options?
Thanks, Andrew
Solution:
In the end, I discovered another similar (but not exact) question here on stackoverflow, and one answer gave a solution to my problem. The way to go about it is simply to have the app display a dummy View as the keyboard, and everything else works the way it should.
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
VIN.inputView = dummyView; // Hide keyboard, but show blinking cursor
It works for UITextField and UITextView and they need to be set to editable.
Upvotes: 22
Views: 12506
Reputation: 31
Swift 5
When you create a UIView without specifying a frame, it defaults to a frame with zero size and origin.
So this:
UIView()
Is the same as:
UIView(frame: .zero)
Which is also the same as:
UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
So it can be done like this:
textInputLabel.inputView = UIView()
Upvotes: 0
Reputation: 957
This is the method that worked for me:
func textFieldDidBeginEditing(_ textField: UITextField) {
if someCondition {
textField.resignFirstResponder()
}
}
Upvotes: 0
Reputation: 183
In Swift 4:
let dummyView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
textInputLabel.inputView = dummyView
textInputLabel.becomeFirstResponder()
Upvotes: 3
Reputation: 811
Solution:
In the end, I discovered another similar (but not exact) question here on stackoverflow, and one answer gave a solution to my problem. The way to go about it is simply to have the app display a dummy View as the keyboard, and everything else works the way it should.
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
VIN.inputView = dummyView; // Hide keyboard, but show blinking cursor
It works for UITextField
and UITextView
and they need to be set to editable.
Upvotes: 28
Reputation: 309
I am using [textfield resignFirstResponder]
in delegate method -(void)textFieldDidBeginEditing:(UITextField *)textField
like this:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if (textField == yourTextField){
[yourTextfield resignFirstResponder];
}
}
So keyboard just have no time to appear
Upvotes: 0
Reputation: 113
The author's code...
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
VIN.inputView = dummyView; // Hide keyboard, but show blinking cursor
(Where 'VIN' is the UITextField variable, and this code is placed in somewhere like viewDidLoad)
...does work to prevent the iPhone/iPad popup keyboard to be displayed.
HOWEVER, that's just the 'display' portion of a keyboard interface. If you have an external keyboard attached such as Apple's physical iPhone/iPad keyboard that plugs into the 30-pin or one of a zillion Bluetooth enabled keyboards (or even using Mac keyboard with simulator), the user can still type into the field WITHOUT the display keyboard popup showing.
Also, even with no keyboard popup shown, the user would still be able to PASTE from the paste-board into the field by double-touching the field and selecting PASTE.
The following ADDITIONAL code prevents those keyboard from being used and prevents PASTE as well:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSLog(@"myXViewController shouldChangeCharactersInRange");
return FALSE;
}
...and still allows selection, but not editing of the text in the field.
Upvotes: 10
Reputation: 3467
Make it a UITextView, and make the UITextView non-editable. That will still allow copying.
EDIT: Yeah my bad I forgot that part... Basically what you did was make a scrollable label.
If you want all the functionality without the keyboard, you'd need to make it editable, and edit UITextFieldDelegate's -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
to return NO
. This will block the keyboard from coming up.
Upvotes: 2