IOS--UITextFiled的使用方法

IOS--UITextFiled的使用方法详细

 

// UITextField的常用方法

    UITextField *oneTextField = [[UITextField alloc] init];

    

    // 最常用

    oneTextField.frame = CGRectMake(30, 30, 260, 35); // 设置位置

    oneTextField.backgroundColor = [UIColor grayColor]; // 设置背景颜色

    oneTextField.alpha = 1.0; // 设置透明度,范围从0.0-1.0之间

    oneTextField.textColor = [UIColor redColor]; // 设置文字的颜色

    oneTextField.text = @"默认的文字"; // 设置默认的文字

    oneTextField.placeholder = @"这是提示文字"; // 显示提示文件,当输入文字时将自动消失

    oneTextField.font = [UIFont boldSystemFontOfSize:25.0f]; // 设置字体的大小

    oneTextField.textAlignment = NSTextAlignmentCenter; // 设置文字的对其方式

    // 对其的样式如下

    //    typedef NS_ENUM(NSInteger, NSTextAlignment) {

//        NSTextAlignmentLeft      = 0,    // Visually left aligned

//#if TARGET_OS_IPHONE

//        NSTextAlignmentCenter    = 1,    // Visually centered

//        NSTextAlignmentRight     = 2,    // Visually right aligned

//#else

//        NSTextAlignmentRight     = 1,    // Visually right aligned

//        NSTextAlignmentCenter    = 2,    // Visually centered

//#endif

//        NSTextAlignmentJustified = 3,    // Fully-justified. The last line in a paragraph is natural-aligned.

//        NSTextAlignmentNatural   = 4,    // Indicates the default alignment for script

//    } NS_ENUM_AVAILABLE_IOS(6_0);

    

    // 设置oneTextField的样式

    oneTextField.borderStyle = UITextBorderStyleRoundedRect; // 设置oneTextField的样式

    oneTextField.clearsOnBeginEditing = YES; // 再次编辑就清空原有的内容

    oneTextField.minimumFontSize = 20.0f; // 设置自动缩小显示的最小字体大小    

    oneTextField.tag = 10001; // 设置oneTextField的标签,主要用在委托方法中

    oneTextField.secureTextEntry = NO; // 设置是否以密码的圆点形式显示

    oneTextField.autocorrectionType = YES; // 设置是否启动自动提醒更新功能

    oneTextField.returnKeyType = UIReturnKeyDefault; // 设置弹出的键盘带形式与带的按键

    // 以下是弹出的键盘的带按钮的全部样式

//    typedef NS_ENUM(NSInteger, UIReturnKeyType) {

//        UIReturnKeyDefault, // 默认的样式

//        UIReturnKeyGo, // 带一个go按钮

//        UIReturnKeyGoogle, // 带一个Search按钮,使用google搜索

//        UIReturnKeyJoin, // 带一个Join按钮,登录时使用

//        UIReturnKeyNext, // 带一个Next按钮,进入下一个输入框

//        UIReturnKeyRoute, // 带一个Route按钮

//        UIReturnKeySearch, // 带一个Search按钮

//        UIReturnKeySend, // 带一个Send按钮

//        UIReturnKeyYahoo, // 带一个Search,使用yahoo搜索

//        UIReturnKeyDone, // 带一个Done按钮

//        UIReturnKeyEmergencyCall, // 带一个emergency call按钮

//    };

 

    

    oneTextField.keyboardType = UIKeyboardTypeNamePhonePad; // 弹出的键盘的样式

    // 以下是所有弹出的键盘的样式

//    typedef NS_ENUM(NSInteger, UIKeyboardType) {

//        UIKeyboardTypeDefault,                // Default type for the current input method.

//        UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active

//        UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation.

//        UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).

//        UIKeyboardTypeNumberPad,              // A number pad (0-9). Suitable for PIN entry.

//        UIKeyboardTypePhonePad,               // A phone pad (1-9, *, 0, #, with letters under the numbers).

//        UIKeyboardTypeNamePhonePad,           // A type optimized for entering a person‘s name or phone number.

//        UIKeyboardTypeEmailAddress,           // A type optimized for multiple email address entry (shows space @ . prominently).

//#if __IPHONE_4_1 <= __IPHONE_OS_VERSION_MAX_ALLOWED

//        UIKeyboardTypeDecimalPad,             // A number pad with a decimal point.

//#endif

//#if __IPHONE_5_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED

//        UIKeyboardTypeTwitter,                // A type optimized for twitter text entry (easy access to @ #)

//#endif

//        

//        UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated

//        

//    };

    

    oneTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; // 输入的对其方法

    oneTextField.autoresizingMask = UIViewAutoresizingFlexibleHeight; // 自动适应高度

    oneTextField.clearButtonMode = UITextFieldViewModeWhileEditing; // 编辑的时候会显示出删除全部的小X

    // 出现删除全部小X的时间

//    typedef enum {

//        UITextFieldViewModeNever // 重不出现

//        UITextFieldViewModeWhileEditing, // 编辑时出现

//        UITextFieldViewModeUnlessEditing, // 除了编辑外都出现

//        UITextFieldViewModeAlways  // 一直出现

//    } UITextFieldViewMode;

    

    //首字母是否大写

    oneTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;

    // 以下是字母大写的几种情况

//    typedef enum {

//        UITextAutocapitalizationTypeNone, // 不自动大写

//        UITextAutocapitalizationTypeWords, // 单词首字母大写

//        UITextAutocapitalizationTypeSentences, // 句子的首字母大写

//        UITextAutocapitalizationTypeAllCharacters, // 所有字母都大写

//    } UITextAutocapitalizationType;

    

    

    //键盘外观

    oneTextField.keyboardAppearance=UIKeyboardAppearanceAlert;

//    typedef enum {

//        UIKeyboardAppearanceDefault, // 默认外观,浅灰色

//        UIKeyboardAppearanceAlert,   // 深灰 石墨色

//    } UIReturnKeyType;

    

    

    //最右侧加图片是以下代码  左侧类似

    UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_action_stat_share.png"]];

    // oneTextField.rightView = image;

    // oneTextField.rightViewMode = UITextFieldViewModeAlways;

    oneTextField.leftView = image;

    oneTextField.leftViewMode = UITextFieldViewModeAlways;

//    typedef enum {

//        UITextFieldViewModeNever,

//        UITextFieldViewModeWhileEditing,

//        UITextFieldViewModeUnlessEditing,

//        UITextFieldViewModeAlways

//    } UITextFieldViewMode;

    

    

    // 要想设置委托,就需要实现协议,需要在.h文件中添加协议

    oneTextField.delegate = self; // 设置委托

 

    // 添加到view上,并释放内存

    [self.view addSubview:oneTextField];

    [oneTextField release], oneTextField = nil;

 

 

//当开始点击textField会调用的方法

-(void)textFieldDidBeginEditing:(UITextField *)textField {

    NSLog(@"开始编辑");

}

//当textField编辑结束时调用的方法

-(void)textFieldDidEndEditing:(UITextField *)textField {

    NSLog(@"结束编辑");

}

//按下Done按钮的调用方法,我们让键盘消失

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];

    return YES;

}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。