Natan R.
Natan R.

Reputation: 5181

UITextField show cursor even when userInteractionEnabled is set to NO

I have a UITextField and it must blink the cursor anyway, even its userInteractionEnabled property is set to NO. I don't want the UITextField to becomeFirstResponder and show the keyboard.

Now you may be asking:

1. If you want to hide the keyboard, why to show the cursor?

A: The problem is that I need to show the user the UITextField is being edited, with a different/custom keyboard.

2. So why don't you use the inputView property?

A: Because the keyboard in inputView comes up from bottom, and I want my custom keyboard in the center of the screen.

So let's go to the real question:

How can I show the cursor? Is there any property I can set? If not, how I would draw a cursor? Making a UIView that gets added and removed with alpha, or subclassing UITextField and overriding drawInRect?

Upvotes: 3

Views: 2317

Answers (1)

andreamazz
andreamazz

Reputation: 4286

You can add a small UIView with a repeting animation that sets the alpha to 0 and 1 back and forth with animateWithDuration:delay:options:animations:completion:.

[UIView animateWithDuration:1 
                      delay:0 
                    options:UIViewAnimationOptionRepeat 
                 animations:^{ [cursorView setAlpha:0]; }
                 completion:^(BOOL animated){ [cursorView setAlpha:1]; } ];

To position the view correctly as a subview of the textfield, in front of the text entered, you can use the NSString method sizewithfont:forWidth:lineBreakMode:

Upvotes: 4

Related Questions