有关UITextField配置的信息

时间:2011-03-24 09:25:50

标签: iphone objective-c

我想了解有关UITextField标准的配置

- (IBAction)add:(id)sender 
{
    UITextField * textfieldToAdd = [[[UITextField alloc] init] autorelease];
    // ... configuration code for textfield ...
    [self.view addSubview:textfieldToAdd];
}

1 个答案:

答案 0 :(得分:1)

以下是Apple的示例UICatalog

中的代码
#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 = @"<enter text>";
        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;
}

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

        textFieldRounded.borderStyle = UITextBorderStyleRoundedRect;
        textFieldRounded.textColor = [UIColor blackColor];
        textFieldRounded.font = [UIFont systemFontOfSize:17.0];
        textFieldRounded.placeholder = @"<enter text>";
        textFieldRounded.backgroundColor = [UIColor whiteColor];
        textFieldRounded.autocorrectionType = UITextAutocorrectionTypeNo;   // no auto correction support

        textFieldRounded.keyboardType = UIKeyboardTypeDefault;
        textFieldRounded.returnKeyType = UIReturnKeyDone;

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

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

        textFieldRounded.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.
        [textFieldRounded setAccessibilityLabel:NSLocalizedString(@"RoundedTextField", @"")];
    }
    return textFieldRounded;
}

- (UITextField *)textFieldSecure
{
    if (textFieldSecure == nil)
    {
        CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);
        textFieldSecure = [[UITextField alloc] initWithFrame:frame];
        textFieldSecure.borderStyle = UITextBorderStyleBezel;
        textFieldSecure.textColor = [UIColor blackColor];
        textFieldSecure.font = [UIFont systemFontOfSize:17.0];
        textFieldSecure.placeholder = @"<enter password>";
        textFieldSecure.backgroundColor = [UIColor whiteColor];

        textFieldSecure.keyboardType = UIKeyboardTypeDefault;
        textFieldSecure.returnKeyType = UIReturnKeyDone;    
        textFieldSecure.secureTextEntry = YES;  // make the text entry secure (bullets)

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

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

        textFieldSecure.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.
        [textFieldSecure setAccessibilityLabel:NSLocalizedString(@"SecureTextField", @"")];
    }
    return textFieldSecure;
}

- (UITextField *)textFieldLeftView
{
    if (textFieldLeftView == nil)
    {
        CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);
        textFieldLeftView = [[UITextField alloc] initWithFrame:frame];
        textFieldLeftView.borderStyle = UITextBorderStyleBezel;
        textFieldLeftView.textColor = [UIColor blackColor];
        textFieldLeftView.font = [UIFont systemFontOfSize:17.0];
        textFieldLeftView.placeholder = @"<enter text>";
        textFieldLeftView.backgroundColor = [UIColor whiteColor];

        textFieldLeftView.keyboardType = UIKeyboardTypeDefault;
        textFieldLeftView.returnKeyType = UIReturnKeyDone;  

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

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

        // Add an accessibility label that describes the text field.
        [textFieldLeftView setAccessibilityLabel:NSLocalizedString(@"CheckMarkIcon", @"")];

        textFieldLeftView.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"segment_check.png"]];
        textFieldLeftView.leftViewMode = UITextFieldViewModeAlways;

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

浏览UITextField类的参考文档也尝试阅读UITextFieldDelegate。查找可以执行的不同任务,并查看可用的方法和属性。并根据您的要求使用它。