Create a text input

August 15, 2008    Programming Snippets

So here is a quick no real code tutorial. This is another thing that took me a bit to figure out.

For this example, I am putting a text input in a TableView. So here is my class declaration.

@interface OptionsViewController : UITableViewController <UITextFieldDelegate> { }

You need to put the UITextFieldDelegate as a reference class. Now I am going to create the text input in the .m file of the same class

// creating the input
self.usernameInput = [[UITextField alloc] initWithFrame:frame];
// setting it to a rounded border
self.usernameInput.borderStyle = UITextBorderStyleRoundedRect;
// text color is black
self.usernameInput.textColor = [UIColor blackColor];
// font size
self.usernameInput.font = [UIFont systemFontOfSize:17.0];
// the default text in light gray
self.usernameInput.placeholder = @"Username";
// background color of the box
self.usernameInput.backgroundColor = [UIColor whiteColor];
// sets up auto correction in case of spelling errors
self.usernameInput.autocorrectionType = UITextAutocorrectionTypeYes;
// shows the done key in the keyboard
self.usernameInput.returnKeyType = UIReturnKeyDone;
// shows the X button in the text input if a user wants to clear it quickly
self.usernameInput.clearButtonMode = UITextFieldViewModeWhileEditing;
// sets the delegate to the controller when we press the done so the controller knows to take control
self.usernameInput.delegate = self;
// adding it to my view
[MyContentView addSubview:self.usernameInput];
// releasing the memory
[self.usernameInput release];

That is about it!

comments powered by Disqus