EzDevInfo.com

uitextfield interview questions

Top uitextfield frequently asked interview questions

Change UITextField and UITextView Cursor / Caret Color

I'm wondering about changing the color of the cursor / caret in a UITextField (And UITextView if its the same answer) in iOS. I've seen answers for OSX development, but nothing for iOS.

Is this even possible?


Source: (StackOverflow)

iPhone Keyboard Covers UITextField

I have an app where, in Interface Builder, I set up a UIView that has a text field near the bottom of the view. When I run the app and try to enter text into that field, the keyboard slides up overtop of the field so I can't see what I'm typing until I hide the keyboard again.

Has anyone else run into this problem and found a good way to solve it without either making the parent view scrollable or moving the text field farther up the screen?


Source: (StackOverflow)

Advertisements

iphone, dismiss keyboard when touching outside of UITextField

I'm wondering how to make the keyboard disappear when the user touches outside of the UITextField?


Source: (StackOverflow)

Text inset for UITextField?

I would like to inset the text of a UITextField. Is this possible? Thanks.


Source: (StackOverflow)

How do I vertically center UITextField Text?

I am simply instantiating a UITextField and noticing that the text doesn't center vertically. Instead, it is flush with the top of my button, which I find kind of odd since I would expect the default to center it vertically. How can I center it vertically, or is there some default setting that I am missing?


Source: (StackOverflow)

iPhone UITextField - Change placeholder text color

I'd like to change the color of the placeholder text I set in my UITextField controls, to make it black.

I'd prefer to do this without using normal text as the placeholder and having to override all the methods to imitate the behaviour of a placeholder.

I believe if I override this method..

- (void)drawPlaceholderInRect:(CGRect)rect

..then I should be able to do this. But I'm unsure how to access the actual placeholder object from within this method.


Source: (StackOverflow)

Set the maximum character length of a UITextField

How can I set the maximum amount of characters in a UITextField on the iPhone SDK when I load up a UIView?


Source: (StackOverflow)

How to make a UITextField move up when keyboard is present

With the iPhone SDK:

I have a UIView with UITextFields that brings up a keyboard. I need it to be able to:

  1. Allow scrolling of the contents of the UIScrollView to see the other text fields once the keyboard is brought up

  2. Automatically "jump" (by scrolling up) or shortening

I know that I need a UIScrollView. I've tried changing the class of my UIView to a UIScrollView but I'm still unable to scroll the textboxes up or down.

Do I need both a UIView and a UIScrollView? Does one go inside the other? [EDIT: I now know that you want a UIView inside of a UIScrollView, and the trick is to programatically set the content size of the UIScrollView to the frame size of the UIView.]

Then what needs to be implemented in order to automatically scroll to the active text field?

Ideally as much of the setup of the components as possible will be done in Interface Builder. I'd like to only write code for what needs it.

Note: the UIView (or UIScrollView) that I'm working with is brought up by a tabbar (UITabBar), which needs to function as normal.


Edit: I am adding the scroll bar just for when the keyboard comes up. Even though it's not needed, I feel like it provides a better interface because then the user can scroll and change textboxes, for example.

I've got it working where I change the frame size of the UIScrollView when the keyboard goes up and down. I'm simply using:

-(void)textFieldDidBeginEditing:(UITextField *)textField { 
    //Keyboard becomes visible
    scrollView.frame = CGRectMake(scrollView.frame.origin.x, 
                     scrollView.frame.origin.y, 
scrollView.frame.size.width,
scrollView.frame.size.height - 215 + 50);   //resize
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
   //keyboard will hide
    scrollView.frame = CGRectMake(scrollView.frame.origin.x, 
       scrollView.frame.origin.y, 
     scrollView.frame.size.width,
      scrollView.frame.size.height + 215 - 50); //resize
}

However this doesn't automatically "move up" or center the lower text fields in the visible area, which is what I would really like.


Source: (StackOverflow)

iPad keyboard will not dismiss if modal ViewController presentation style is UIModalPresentationFormSheet

Note:

See accepted answer (not top voted one) for solution as of iOS 4.3.

This question is about a behavior discovered in the iPad keyboard, where it refuses to be dismissed if shown in a modal dialog with a navigation controller.

Basically, if I present the navigation controller with the following line as below:

navigationController.modalPresentationStyle = UIModalPresentationFormSheet;

The keyboard refuses to be dismissed. If I comment out this line, the keyboard goes away fine.

...

I've got two textFields, username and password; username has a Next button and password has a Done button. The keyboard won't go away if I present this in a modal navigation controller.

WORKS

broken *b = [[broken alloc] initWithNibName:@"broken" bundle:nil];
[self.view addSubview:b.view];

DOES NOT WORK

broken *b = [[broken alloc] initWithNibName:@"broken" bundle:nil];
UINavigationController *navigationController = 
[[UINavigationController alloc]
 initWithRootViewController:b];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
[b release];

If I remove the navigation controller part and present 'b' as a modal view controller by itself, it works. Is the navigation controller the problem?

WORKS

broken *b = [[broken alloc] initWithNibName:@"broken" bundle:nil];
b.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:b animated:YES];
[b release];

WORKS

broken *b = [[broken alloc] initWithNibName:@"broken" bundle:nil];
UINavigationController *navigationController = 
    [[UINavigationController alloc]
         initWithRootViewController:b];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
[b release];

Source: (StackOverflow)

How do you dismiss the keyboard when editing a UITextField

I know that I need to tell my UITextField to resign first responder when I want to dismis the keyboard, but I'm not sure how to know when the user has pressed the "Done" key on the keyboard. Is there a notification I can watch for?


Source: (StackOverflow)

How to create a multiline UITextfield?

I am developing an application where user has to write some information. For this purpose I need a UITextField which is multi-line (in general UITextField is a single line).

As I'm Googling I find a answer of using UITextView instead of UITextfield for this purpose.


Source: (StackOverflow)

Having a UITextField in a UITableViewCell

I'm trying to do that for a couple of days now, and after reading tons of messages of people trying to do that too, I'm still unable to have a fully working UITextField in some of my UITableViewCells, just like in this example:

Screenshot

Either I have the form working but the text is not visible (although I set its color to blue), the keyboard goes on the field when I click on it and I haven't been able to correctly implement the keyboard events. I tried with a bunch of examples from Apple (mainly UICatalog, where there is a kinda similar control) but it's still not working correctly.

Can somebody help me (and all the people trying to realize this control) and post a simple implementation of a UITextField in a UITableViewCell, that works fine?


Source: (StackOverflow)

Super slow lag/delay on initial keyboard animation of UITextField

Alright, this problem has been driving me nuts.

It takes roughly 3-4 seconds for the keyboard to pop up after I touch my UITextField. This only occurs on the first time the keyboard pops up since the app launched, afterwards the animation starts instantly.

At first I thought it was problem of loading too many images, or my UITableView, but I just created a brand new project with only a UITextField, and I still experience this problem. I'm using ios5, xcode ver 4.2, and running on an iphone 4s.

This is my code:

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 280, 30)];
  textField.borderStyle = UITextBorderStyleRoundedRect;
  textField.delegate = self;
  [self.view addSubview:textField];
}

@end

Is this a common problem for all apps?

Right now, the only way I can make it somewhat better is by having the textField become/resign first responder in viewDidAppear, but that doesn't solve the problem entirely - it just loads the delay onto when the view loads instead. If I click on the textField immediately when the view loads I still get the problem; if I wait 3-4 seconds after the view loads before touching the textField, I don't get the delay.


Source: (StackOverflow)

Getting the Value of a UITextField as keystrokes are entered?

Let's say I have the following code:

IBOutlet UITextField* nameTextField;
IBOutlet UILabel* greetingLabel;

I'd like the greetingLabel to read "Hello [nameTextField]" as soon as the user presses any key.

What I need basically is the iPhone equivalent of the Cocoa delegate method controlTextDidChange.

The textField:shouldChangeCharactersInRange: delegate method is called each time a keystroke occurs:

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

The string argument returns the character that is pressed. The actual textField's value (nameTextField.text) remains blank however.

What am I missing here? (I'd like nameTextField to reflect the exact string that the user has entered so far).


Source: (StackOverflow)

How to make return key on iPhone make keyboard disappear?

I have two UITextFields (e.g. username and password) but I cannot get rid of the keyboard when pressing the return key on the keyboard. How can I do this?


Source: (StackOverflow)