IOS--UITextFiled的使用方法

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Override point for customization after application launch.

self.window.backgroundColor = [UIColor whiteColor];

[self.window makeKeyAndVisible];

//创建左视图的label

UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];

leftLabel.text = @"用户名";

leftLabel.backgroundColor = [UIColor cyanColor];

leftLabel.textAlignment = NSTextAlignmentCenter;

//输入框

//1,申请空间

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(60, 100, 200, 30)];

//2,设置属性

textField.tag = 1001;

textField.borderStyle = UITextBorderStyleRoundedRect;//样式

//textField.text = @"输入";           //默认输入

//  textField.placeholder = @"在此输入...";//输入提示

textField.textAlignment = NSTextAlignmentCenter;//居中

//  textField.secureTextEntry = YES;                //安全输入

textField.keyboardType = UIKeyboardTypeEmailAddress;//键盘样式

textField.returnKeyType = UIReturnKeySearch;//键盘返回

textField.textColor = [UIColor redColor]; //字体颜色

textField.clearsOnBeginEditing = YES;     //输入时清空,(密码)

textField.clearButtonMode = UITextFieldViewModeWhileEditing;//编辑

textField.delegate = self;  //设置代理 当前self代表appdelegate

textField.leftView = leftLabel;//左视图

textField.leftViewMode = UITextFieldViewModeAlways;//左视图样式

[leftLabel release];

//3,添加

[_window addSubview:textField];

//4,释放

[textField release];

//按钮

//静态方法创建,不需要是释放

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

//属性

button.frame = CGRectMake(50, 200, 80, 30);

[button setTitle:@"提交" forState:UIControlStateNormal];//显示文字

[button setTitle:@"哇" forState:UIControlStateHighlighted];//显示文字

[button setBackgroundImage:[UIImage  imageNamed:@"1.jpg"] forState:UIControlStateNormal];

//[button setBackgroundColor:[UIColor greenColor]];

//绑定事件

[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

//移除事件

[_window addSubview:button];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60, 50, 150, 30)];

[_window addSubview:label];

[label release];

return YES;

}

- (void)buttonAction:(UIButton *)sender

{

NSLog(@"不要点我");

//通过tag 取出视图

UITextField  *textFiled = (UITextField *)[_window viewWithTag:1001];

NSLog(@"%@",textFiled.text);

//sender.backgroundColor = [UIColor redColor];

}

#pragma mark - UITextFieldDelegate

//已经开始编辑

- (void)textFieldDidBeginEditing:(UITextField *)textField

{

NSLog(@"已经开始编辑");

}

//已经结束编辑

- (void)textFieldDidEndEditing:(UITextField *)textField

{

NSLog(@"已经结束编辑");

}

//按下return

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

NSLog(@"按下return");

//释放输入框 第一相应者

[textField resignFirstResponder];

return YES;

}

//监听文本框值的改变

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

return YES;

}

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;

}

上一篇:.NET中JSON的序列化和反序列化


下一篇:Nginx核心流程及模块介绍