iOS总结控件三(UItextField)

来源:互联网 发布:图像识别算法 知乎 编辑:程序博客网 时间:2024/05/30 12:30

UItextField

1.创建一个UItextField实例

   UITextField *textField = [[UITextFieldalloc]init];

    textField.frame =CGRectMake(10,40,self.view.frame.size.width -20,40);

    textField.backgroundColor = [UIColorlightGrayColor];

2.设置textFiled中得文字

   textField.text = @"用户名";

3.设置textFiled的字体

    textField.font = [UIFontsystemFontOfSize:20];

4.设置textFiled的文字颜色

textField.textColor = [UIColorpurpleColor];

5.设置textFiled的对齐方式

    textField.textAlignment =NSTextAlignmentLeft;

6.设置textFiled的样式

textField.borderStyle =UITextBorderStyleNone;

7.设置textFiled的圆角

textField.layer.cornerRadius =4.0f;

8.设置textFiled边框的宽度

textField.layer.borderWidth =1;

9.设置textFiled边框的颜色

textField.layer.borderColor = [UIColordarkGrayColor].CGColor;

10.设置聊天框的背景图片

textField.background = image;

11.设置聊天框的默认文字

textField.placeholder = @"用户名”;(灰度)

12.设置聊天框的attributedPlaceholder

NSMutableAttributedString *muAttStr = [[NSMutableAttributedStringalloc]initWithString:@"用户名"];

[muAttStr addAttribute:NSForegroundColorAttributeNamevalue:[UIColorblueColor]range:NSMakeRange(0, muAttStr.length)];

textField.attributedPlaceholder = muAttStr;

13.设置聊天框的清除全部按钮显示模式

textField.clearButtonMode =UITextFieldViewModeWhileEditing;

14设置聊天框的左边视图

UIView *leftView = [[UIView alloc] init];

leftView.backgroundColor = [UIColor clearColor];

leftView.frame = CGRectMake(0, 0, 50, 40);

textField.leftView = iconImgView;(iconImgView这是一个我们建立好的imageview)

15.设置聊天框左边视图的出现方式

    textField.leftViewMode =UITextFieldViewModeAlways;

16.键盘右下角的按钮,类型

textField.returnKeyType =UIReturnKeyDone;

    [self.viewaddSubview:textField];

17.把文本框设置为第一响应者(当设置一个控件为第一响应者之后,在设置其他为第一响应者,无效)

[textField becomeFirstResponder];

18.设置密文模式

passwordTF.secureTextEntry =YES;

19.设置键盘类型

 passwordTF.keyboardType =UIKeyboardTypeNumberPad;

20.如果一个页面有多个UITextField,调用下面的方法,收起全部键盘

[self.viewendEditing:YES];

21.textFieldDidBeginEditing是在当UITextField成为第一响应者的时候调用

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

22.我们判断UITextField是否应该成为第一响应者

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

    return YES;

    如果返回值是NO的话,UITextField将不会成为第一响应者,接下来的textFieldDidBeginEditing这个方法将不会被调用

}

23.我们判断textField是否要放弃第一响应者,收起键盘

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField

{

    return YES;

}

24.已经收起键盘,放弃第一响应者

- (void)textFieldDidEndEditing:(UITextField *)textField

{


}

25这个方法是当UITextField中得字符改变的时候调用,

如果是新增字符,range这个字段将为(x0),如果是替换则不是

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

{

    常用用法:例如,判断输入内容是否为一个电话号码

    1、如果输入的字符不是我们想要的字符我们返回no

    2、如果要展示在文本框里的字数string的长度超过我们的限制,1)截取字符串 2)返回NO

    return YES;

}


26.当文本框的清楚全部被点击时候调用,如果返回yes则允许清空,如果no则不允许

- (BOOL)textFieldShouldClear:(UITextField *)textField

{

    return YES;

}

27.当键盘右下角按钮点击的时候调用

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    textField,放弃第一响应者,收起键盘

    [textField resignFirstResponder];

    最好在这里在进行一次文本框中字符串的校验

    有可能进行网络请求和其他的操作

    return YES;

}


0 0
原创粉丝点击