UITextField show cursor even when userInteractionEnabled is set to NO

北城以北 提交于 2019-12-02 12:01:53

问题


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?


回答1:


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:



来源:https://stackoverflow.com/questions/12128509/uitextfield-show-cursor-even-when-userinteractionenabled-is-set-to-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!