Sunday, June 2, 2013

UITextField UITextFieldViewModeWhileEditing example in Objective C (iOS).


UITextField UITextFieldViewModeWhileEditing

UITextFieldViewMode
Defines the times at which overlay views appear in a text field.

typedef enum {
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
} UITextFieldViewMode;

Constants
UITextFieldViewModeNever
The overlay view never appears.
UITextFieldViewModeWhileEditing
The overlay view is displayed only while text is being edited in the text field.
UITextFieldViewModeUnlessEditing
The overlay view is displayed only when text is not being edited.
UITextFieldViewModeAlways
The overlay view is always displayed.

UITextField UITextFieldViewModeWhileEditing example.
- (BOOL)becomeFirstResponder
{
    BOOL ret = YES ;

    ret = [super becomeFirstResponder] ;

    if( ret && ( _setupClearButtonMode == UITextFieldViewModeWhileEditing ) )
        self.rightViewMode = UITextFieldViewModeAlways ;

    return ret ;
}

- (BOOL)resignFirstResponder
{
    BOOL ret = YES ;

    ret = [super resignFirstResponder] ;

    if( ret && ( _setupClearButtonMode == UITextFieldViewModeWhileEditing ) )
        self.rightViewMode = UITextFieldViewModeWhileEditing ;

    return ret ;
}

Example of [UITextField UITextFieldViewModeWhileEditing].
cell = [tableView dequeueReusableCellWithIdentifier:@"CellNameIdentifier"];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"CellName" owner:self options:nil];
cell = cellName;
[cellName setAccessoryType:UITableViewCellAccessoryNone];
        //textfield in inside cellName
textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
}

UITextField UITextFieldViewModeWhileEditing example.
#pragma mark -
#pragma mark Text Fields

- (UITextField *)textFieldNormal
{
    if (textFieldNormal == nil)
    {
        CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);
        textFieldNormal = [[UITextField alloc] initWithFrame:frame];

        textFieldNormal.borderStyle = UITextBorderStyleBezel;
        textFieldNormal.textColor = [UIColor blackColor];
        textFieldNormal.font = [UIFont systemFontOfSize:17.0];
        textFieldNormal.placeholder = @"";
        textFieldNormal.backgroundColor = [UIColor whiteColor];
        textFieldNormal.autocorrectionType = UITextAutocorrectionTypeNo;    // no auto correction support

        textFieldNormal.keyboardType = UIKeyboardTypeDefault;   // use the default type input method (entire keyboard)
        textFieldNormal.returnKeyType = UIReturnKeyDone;

        textFieldNormal.clearButtonMode = UITextFieldViewModeWhileEditing;  // has a clear 'x' button to the right

        textFieldNormal.tag = kViewTag;     // tag this control so we can remove it later for recycled cells

        textFieldNormal.delegate = self;    // let us be the delegate so we know when the keyboard's "Done" button is pressed

        // Add an accessibility label that describes what the text field is for.
        [textFieldNormal setAccessibilityLabel:NSLocalizedString(@"NormalTextField", @"")];
    }  
    return textFieldNormal;
}

End of UITextField UITextFieldViewModeWhileEditing example article.