0119——UITextField

时间:2022-10-11 23:31:03

1.默认文本

   _loginTextField.placeholder = @"QQ号/手机";

2.设置边框

  _loginTextField.borderStyle = UITextBorderStyleRoundedRect;

  typedef enum {

    UITextBorderStyleNone,

    UITextBorderStyleLine,

    UITextBorderStyleBezel,

    UITextBorderStyleRoundedRect

  } UITextBorderStyle;

3.设置键盘类型

_loginTextField.keyboardType = UIKeyboardTypeDefault;

  typedef enum {

  UIKeyboardTypeDefault,       默认键盘,支持所有字符

  UIKeyboardTypeASCIICapable,  支持ASCII的默认键盘

  UIKeyboardTypeNumbersAndPunctuation,  标准电话键盘,支持+*#字符

  UIKeyboardTypeURL,            URL键盘,支持.com按钮 只支持URL字符

      UIKeyboardTypeNumberPad,              数字键盘

      UIKeyboardTypePhonePad,     电话键盘

  UIKeyboardTypeNamePhonePad,   电话键盘,也支持输入人名

      UIKeyboardTypeEmailAddress,   用于输入电子 邮件地址的键盘

      UIKeyboardTypeDecimalPad,     数字键盘 有数字和小数点

UIKeyboardTypeTwitter,        优化的键盘,方便输入@、#字符

UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,

  } UIKeyboardType;

4.更改键盘下方的ruturn

_loginTextField.returnKeyType = UIReturnKeyDone;

  typedef enum {

  UIReturnKeyDefault, 默认 灰色按钮,标有Return

  UIReturnKeyGo,      标有Go的蓝色按钮

  UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索

  UIReturnKeyJoin,标有Join的蓝色按钮

  UIReturnKeyNext,标有Next的蓝色按钮

  UIReturnKeyRoute,标有Route的蓝色按钮

  UIReturnKeySearch,标有Search的蓝色按钮

  UIReturnKeySend,标有Send的蓝色按钮

    UIReturnKeyYahoo,标有Yahoo的蓝色按钮

    UIReturnKeyYahoo,标有Yahoo的蓝色按钮

    UIReturnKeyEmergencyCall, 紧急呼叫按钮

  } UIReturnKeyType;

5.小叉叉

_loginTextField.clearButtonMode = UITextFieldViewModeWhileEditing;

6.设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动

textFied.adjustsFontSizeToFitWidth = YES;

设置自动缩小显示的最小字体大小

text.minimumFontSize = 20;

7.再次编辑就清空

text.clearsOnBeginEditing = YES;

8.UITextFieldViewMode

  typedef enum {

  UITextFieldViewModeNever,  重不出现

  UITextFieldViewModeWhileEditing, 编辑时出现

  UITextFieldViewModeUnlessEditing,  除了编辑外都出现

  UITextFieldViewModeAlways   一直出现

  } UITextFieldViewMode;

9.键盘外观

   textView.keyboardAppearance=UIKeyboardAppearanceDefault;

   typedef enum {

      UIKeyboardAppearanceDefault, 默认外观,浅灰色

    UIKeyboardAppearanceAlert,     深灰 石墨色

   } UIReturnKeyType;

10.左边小图,放大镜或者解锁图案

  UIImage * image = [UIImage imageNamed:@"search"];

UIImageView * imageview =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];

imageview.image = image;

_loginTextField.leftView = imageview;

_loginTextField.leftViewMode = UITextFieldViewModeAlways;//放大镜一直出现

11.return键隐藏键盘

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

//点击textField为第一响应者,键盘弹出

//取消textField第一响应者

[textField resignFirstResponder];

//[textField becomeFirstResponder];

return YES;

}

12.设置代理(状态改变回调相应的方法)

_loginTextField.delegate =self;

定义了一套代理用来监听控件的状态变化

@interface ViewController : UIViewController<UITextFieldDelegate>