UITextField - OC

textField

屏幕快照 2017-02-05 下午9.18.19.png

textField定义

//定义一个textField
//文本输入区域
//例如:用户名,密码等需要输入文本文字的内容区域
//只能输入单行的文字,不能输入或显示多行
UITextField *_textField;

@property (retain,nonatomic)UITextField * textField;

textField初始化

//创建一个文本输入区对象
//这里 self.textField == _textField
self.textField = [[UITextField alloc]init];

textField 的位置设置

//设置文本输入区的位置
self.textField.frame = CGRectMake(100, 200, 200, 40);

textField提示文字的显示

//设置textField的内容文字
//self.textField.text = @"请输入中文/拼音/简拼";
//如何获取用户输入的内容? NSString *inPut = self.textField.text; 
self.textField.text = @"";
//提示文字信息
//当text属性为空,显示此条信息,否则显示text的字符串
//浅灰色提示文字
self.textField.placeholder = @"请输入中文/拼音/简拼";

textField风格设置

//设置文字的字体大小
self.textField.font = [UIFont systemFontOfSize:12];
//设置字体的颜色
self.textField.textColor = [UIColor grayColor];
//设置边框的风格
//UITextBorderStyleRoundedRect:圆角风格
//UITextBorderStyleLine:线框风格
//UITextBorderStyleNone:无边框风格
//UITextBorderStyleBezel:bezel线框
self.textField.borderStyle = UITextBorderStyleRoundedRect;
//设置虚拟键盘风格
//UIKeyboardTypeDefault:默认风格
//UIKeyboardTypeNamePhonePad:字母和数字组合风格
//UIKeyboardTypeNumberPad:纯数字风格
self.textField.keyboardType = UIKeyboardTypeDefault;
//是否作为密码输入
//YES:作为密码处理,原点加密
//NO:显示输入的文字
self.textField.secureTextEntry = NO;

textField显示出来

[self.view addSubview:self.textField];

textField协议实现

@interface ViewController : UIViewController<UITextFieldDelegate>

//设置代理对象
self.textField.delegate = self;

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //使虚拟键盘回收,不再作为第一消息响应
    //会把self.textField.text 字符串置为空的应该是self.textField.text = @"";
    [self.textField resignFirstResponder];
}

//是否可以进行输入
//是否返回值为YES,可以进行输入,默认为YES
//NO:不能输入
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self.textField.text = @"";
    NSLog(@"编辑输入结束");
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"开始编辑了!");
}

验证输入账号密码是否正确

- (void)pressLogin
{
  NSString * strName = @"sdemon";
  NSString * strPass = @"123456";

  //获取输入框中的用户名文字
  NSString *strTextName = _tfUserName.text;
  NSString *strTextPass = _tfPassWord.text;

  if ([strName isEqualToString:strTextName] && [strPass isEqualToString:strTextPass])
  {
    NSLog(@"用户名密码正确");
    UIAlertView *alView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"验证成功,登录成功" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
    
    [alView show];
  }
  else
  {
    UIAlertView *alView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"验证失败,登录失败" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
    
    [alView show];
  } 
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容