darshan
darshan

Reputation: 161

How do I hide the textfield cursor on the iPhone when keyboard is active

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

Upvotes: 4

Views: 6760

Answers (4)

Manuela Caliga
Manuela Caliga

Reputation: 1

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
    

Upvotes: 0

RVN
RVN

Reputation: 4187

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

Upvotes: 0

mvds
mvds

Reputation: 47134

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.

Upvotes: 1

Peter Hosey
Peter Hosey

Reputation: 96393

Try setting the field's secureTextEntry property to YES.

Upvotes: 2

Related Questions