22

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.

Andrew Hoyer
  • 801
  • 1
  • 8
  • 13

6 Answers6

28

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.

DD_
  • 7,230
  • 11
  • 38
  • 59
Andrew Hoyer
  • 801
  • 1
  • 8
  • 13
  • If you do this in iOS 7, you get a white line as this question http://stackoverflow.com/questions/20946524/white-line-on-the-bottom-on-ios-7-with-uitextfield .Do you know the solution for it? – deltaaruna Jan 06 '14 at 09:53
  • 1
    Looks like someone answered that question, suggesting that the 1 pixel rect is the problem, and this might resolve it: CGRectMake(0, 0, 0, 0). – Andrew Hoyer Jan 17 '14 at 20:08
  • U may use [UIView new] to create view with zero frame. – Dren May 11 '15 at 15:54
10

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.

yagrax
  • 113
  • 3
  • 1
    Good point about the external keyboard -- certainly a consideration in some cases. As for the Paste functionality, that was the whole point of the question -- to hide the keyboard and still allow the copy/paste functions. – Andrew Hoyer Feb 17 '12 at 18:02
  • Thanks for, **Where 'VIN' is the UITextField variable, and this code is placed in somewhere like viewDidLoad)**!! – rptwsthi Dec 06 '13 at 12:14
3

In Swift 4:

let dummyView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
textInputLabel.inputView = dummyView
textInputLabel.becomeFirstResponder()
Elias Pedersen
  • 173
  • 1
  • 1
  • 9
2

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.

dgund
  • 3,459
  • 4
  • 39
  • 64
  • 1
    Thanks. That did the trick. Much appreciated after a lot of searching and nothing I could find to indicate the UITextView would be any better. – Andrew Hoyer Jan 09 '12 at 03:17
  • When I first implemented this, I thought it was okay. Now, however, when I look again, I am only able to Copy. Paste is not allowed, probably because it's an edit. Any other thoughts? – Andrew Hoyer Jan 10 '12 at 21:03
  • I've still been unable to make this work. I tried making it a UITextView with UITextViewDelegate's textViewShouldBeginEditing, and UITextField with UITextFieldDelegate's textFieldShouldBeginEditing. In both cases, when return NO is used, it not only blocks the keyboard, but all editing functions. – Andrew Hoyer Jan 14 '12 at 18:55
  • Okay instead of returning no you'll need to edit the keyboard methods to hide it. Off the top of my head, you could make a scrollview to scroll up, hiding the keyboard below. By making the scrollview not allow user interaction, it will hide the keyboard. If you can think of a better way, do that instead. – dgund Jan 15 '12 at 03:02
  • 1
    Devin, thanks for taking the time to try to help with this. I did a bunch of looking around and testing, and my final solution is added to my initial question. I appreciate your help. – Andrew Hoyer Jan 15 '12 at 08:05
  • You're very welcome, and I'm glad I could eventually help resolve this. – dgund Jan 16 '12 at 04:05
0

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

Asike
  • 309
  • 5
  • 14
0

This is the method that worked for me:

func textFieldDidBeginEditing(_ textField: UITextField) {
    if someCondition {
        textField.resignFirstResponder()
    }
}
Claytog
  • 618
  • 8
  • 8