当键盘处于活动状态时,如何隐藏 iPhone 上的文本字段光标

发布于 2024-09-12 05:52:41 字数 37 浏览 4 评论 0原文

我们正在尝试使用此方法进行密码设置,以便用户不应该看到光标。

we are trying this for password settings,so that cursor should not be visible to user.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凶凌 2024-09-19 05:52:41

编辑:要回答这个问题,隐藏光标最简单的方法是用另一个UITextField覆盖UITextField,其中后面的实际上是第一个响应者,前面的响应者至少会接收焦点(使用 -(void)textFieldDidBeginEditing:(UITextField *)textField 并传递焦点)。 准确地在

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
                replacementString:(NSString *)string

前面的 UITextField 上应用替换,使用类似

front.text = [front.text stringByReplacingCharactersInRange:range withString:string];

You 的东西可能会以这种方式破坏复制和粘贴功能。另外,在这些委托函数中执行的操作要非常小心。期待意想不到的事情。

我原来的答案,同样适用:


我不知道在这种情况下“高级安全性”意味着什么,但看看

更改 uitextfield 中的安全密码字符

要进一步破坏用户体验,并可能添加更多“安全性”,您可以:

  • 通过返回 禁用退格功能NO if [string length] == 0
  • 为输入的每个字符添加 1+(arc4rand()%4) 个字符(如在某些窗口系统上看到的)
  • 添加随机字符而不是恒定的占位符字符

,但我必须说我真的不明白这一切的意义。

edit: to answer the question though, hiding the cursor is done easiest by overlaying the UITextField with another UITextField, where the back one is actually the first responder, and the front one acts to at least receive focus (using -(void)textFieldDidBeginEditing:(UITextField *)textField, and passing focus on). In

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
                replacementString:(NSString *)string

apply the replacement on the front UITextField exactly, using something like

front.text = [front.text stringByReplacingCharactersInRange:range withString:string];

You will probably break copy&paste functionality this way. Also, be very careful what you do in those delegate functions. Expect the unexpected.

My original answer, equally applicable:


I don't know what "advanced security" in this context would mean, but have a look at

change the secure password character in uitextfield

To ruin the user experience even more, and maybe add more "security", you could:

  • disable backspace functionality by returning NO if [string length] == 0
  • add 1+(arc4rand()%4) characters for every character entered (as seen on some windowing systems)
  • add random characters instead of a constant placeholder character

but I must say that I really don't see the point of all this.

半寸时光 2024-09-19 05:52:41

您可以使用隐藏的虚拟文本字段,并使用 TextField 的委托 textDidChage 并使用该值填充实际密码字段中的屏蔽值。

在 Interface Builder 中有一个隐藏的虚拟文本字段,为该字段维护一个 IBOutlet,比如

IBOutlet UITextField *passwordField;
IBOutlet UItextField *dummyField;

还为此设置委托并编写以下委托方法

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString *dummyText = dummyField.text;
NSString *changedText = @"";
if(dummyField == textField)
{
    if([string length] == 0)
    {
        if([dummyText length] <= 1)
        {
            changedText = @"";
        }
        else
        {
            changedText = [NSString stringWithFormat:@"%@",[dummyText substringToIndex:[dummyText length] - 1]]; 
        }
    }
    else
    {
        changedText = [NSString stringWithFormat:@"%@%@",dummyText,string];
    }
    passwordField.text = changedText;
}
return YES; }

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if(passwordField == textField)
{
    [dummyField becomeFirstResponder];
    return NO;
}
return YES; }


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES; }

You can use dummy text field which is hidden and use the delegate textDidChage for TextField and use this value to fill the Masked Values in actual Password field.

In Interface Builder have a dummy textfield which is hidden, maintain a IBOutlet for that say

IBOutlet UITextField *passwordField;
IBOutlet UItextField *dummyField;

Also set delegate for this and write the following delegate methods

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString *dummyText = dummyField.text;
NSString *changedText = @"";
if(dummyField == textField)
{
    if([string length] == 0)
    {
        if([dummyText length] <= 1)
        {
            changedText = @"";
        }
        else
        {
            changedText = [NSString stringWithFormat:@"%@",[dummyText substringToIndex:[dummyText length] - 1]]; 
        }
    }
    else
    {
        changedText = [NSString stringWithFormat:@"%@%@",dummyText,string];
    }
    passwordField.text = changedText;
}
return YES; }

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if(passwordField == textField)
{
    [dummyField becomeFirstResponder];
    return NO;
}
return YES; }


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES; }
老娘不死你永远是小三 2024-09-19 05:52:41

我也遇到了这个问题。我使用了所描述的解决方案
此处

它替换了具有自定义 UILabel 的 UITextField 进行以下调整:

  1. 覆盖属性 inputView 和 inputViewAccessory 以允许设置当标签成为第一响应者时显示的键盘视图。
  2. 
    
        @interface PRLabel : UILabel     
    
        @property (strong, nonatomic, readwrite) UIView* inputView;
        @property (strong, nonatomic, readwrite) UIView* inputAccessoryView;
    
        @end
    
    
  3. 要使标签响应触摸事件,请重写方法 isUserInteractionEnabled 以始终返回 YES。另外,重写方法 TouchesEnded 以使标签成为检测到触摸时的第一响应者。
  4. 重写 canBecomeFirstResponder 方法以始终返回 YES。这将允许输入视图出现。
  5. 
        @implementation PRLabel
        -(id)initWithFrame:(CGRect)frame
        {
        self = [super initWithFrame:frame];
        if (self) {
        // Initialization code
        }
        return self;
        }
    
        -(BOOL)isUserInteractionEnabled
        {
        return YES;
        }
    
        -(BOOL)canBecomeFirstResponder
        {
        return YES;
        }
    
        -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
        {
        [self becomeFirstResponder];
        }
        @end
    

I had this problem too.I used the solution described
here

It replaces the UITextField with a custom UILabel with the following adjustments:

  1. Override the properties inputView and inputViewAccessory to allow setting the keyboard view to appear when the label becomes the first responder.
  2. 
    
        @interface PRLabel : UILabel     
    
        @property (strong, nonatomic, readwrite) UIView* inputView;
        @property (strong, nonatomic, readwrite) UIView* inputAccessoryView;
    
        @end
    
    
  3. To make the label respond to touch events, override the method isUserInteractionEnabled to always return YES. Also, override the method touchesEnded to make the label the first responder when a touch is detected.
  4. Override the method canBecomeFirstResponder to always return YES. This will allow the input view to come up.
  5. 
        @implementation PRLabel
        -(id)initWithFrame:(CGRect)frame
        {
        self = [super initWithFrame:frame];
        if (self) {
        // Initialization code
        }
        return self;
        }
    
        -(BOOL)isUserInteractionEnabled
        {
        return YES;
        }
    
        -(BOOL)canBecomeFirstResponder
        {
        return YES;
        }
    
        -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
        {
        [self becomeFirstResponder];
        }
        @end
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文