即使userInteractionEnabled设置为NO,UITextField也会显示光标 [英] UITextField show cursor even when userInteractionEnabled is set to NO

查看:115
本文介绍了即使userInteractionEnabled设置为NO,UITextField也会显示光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITextField,它必须闪烁光标,即使它的userInteractionEnabled属性设置为NO。我不希望UITextField成为第一个响应者并显示键盘。

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.

现在你可能会问:

1。如果你想隐藏键盘,为什么要显示光标?

答:问题是我需要向用户显示正在编辑的UITextField ,使用不同/自定义键盘。

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

2。那么为什么不使用inputView属性呢?

答:因为inputView中的键盘来自底部,我希望我的自定义键盘在屏幕中心。

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:

我该如何展示光标?我可以设置任何财产吗?如果没有,我将如何绘制光标?制作一个用alpha添加和删除的UIView,或者子类化UITextField并覆盖drawInRect?

推荐答案

你可以添加带有重复动画的小型UIView,使用 animateWithDuration来设置alpha和0来回:延迟:选项:动画:完成:

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]; } ];

要将视图正确定位为文本字段的子视图,在输入的文本前面,您可以使用 NSString 方法 sizewithfont:forWidth:lineBreakMode:

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:

这篇关于即使userInteractionEnabled设置为NO,UITextField也会显示光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆