Programmatically Add Default UITextField



- (UITextField *)getCustomTextField: (NSString *)placeholderText xLoc:(int)X
                               yLoc:(int)Y width:(int)width height:(int)height
    // Start by allocating an ugly default text field
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(X, Y, width, height)];
    // Set font to default text field font
    textField.font = [UIFont fontWithName:@"Helvetica Neue" size:14];
    // If the man wants some placeholder text, give it to him
    if (placeholderText != nil)
        textField.placeholder = placeholderText;
    // Add the clear button while editing
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    // Design our border so it looks like a "real" UITextField
    [textField.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.4] CGColor]];
    [textField.layer setBorderWidth:0.5];
    // Make that sexy corner all round
    textField.layer.cornerRadius = 7;
    textField.clipsToBounds = YES;
    // Add padding to the box so it looks real
    UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 9, 0)];
    textField.leftView = paddingView;
    textField.leftViewMode = UITextFieldViewModeAlways;
    return textField;


Leave a Reply
