码迷,mamicode.com
首页 > 移动开发 > 详细

IOS代码布局(三) UITextField

时间:2016-09-12 00:37:50      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

(一)常规操作

  1.定义一个UITextField,名为textField:

UITextField *textField = = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 200, 40)];

   2.设置背景颜色

textField.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0 alpha:0.5];

  3.??????设置边框类型

textField.borderStyle = UITextBorderStyleRoundedRect; 

  4.设置背景图片

textField???.background = [UIImage imageNamed:@""];

  5.设置委托对象

textField.delegate = self;                                                   

  6.设置默认提示文字

textField.placeholder = @"请输入文字";

  7.弹出键盘(变成第一响应者)

[textField becomeFirstResponder];

  8.文字内容 垂直居中

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

 

(二)委托设置

  1.在声明中引入委托名( ViewController.m中)

@interface ViewController ()<UITextFieldDelegate/*这里引入UITextField 需要的委托*/>{
    //在这里声明全局 私有变量
    UITextField *textField;
}

  2.设置委托对象

textField.delegate = self;  

  3.实现委托方法

//以下是UITextFieldDelegate 的部分委托实现
//*******************************************************************************
#pragma mark -------------------
#pragma mark UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    // return NO to disallow editing.
    return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    // became first responder
    // 在这里监听UITextField becomeFirstResponder事件
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
    // 在这里监听UITextField resignFirstResponder事件
}
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    // return NO to not change text
    NSLog(@"inputText: %@", textField.text);
    return 

 

IOS代码布局(三) UITextField

标签:

原文地址:http://www.cnblogs.com/Asssjc/p/5863133.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!