博客> UITextField详解
UITextField详解
2019-11-15 20:35 评论:0 阅读:1447 内牛满面ing
ios

1.初始化textfield并设置位置及大小

UITextField *textField=[[[UITextFieldalloc]initWithFrame:CGRectMake(100,100,50,50)]autorelease];

2.设置输入框的背景颜色

textField.backgroundColor=[UIColorblueColor]; 3.设置边框样式

typedefNS_ENUM(NSInteger, UITextBorderStyle) { UITextBorderStyleNone, UITextBorderStyleLine, //直线 UITextBorderStyleBezel, UITextBorderStyleRoundedRect //圆角 }; textField.borderStyle=UITextBorderStyleRoundedRect; 4.设置输入框的字体样式和大小

textField.font=[UIFontfontWithName:@"Arial"size:12]; 5.设置字体颜色

textField.textColor=[UIColorredColor]; 

6.设置输入框中的叉号在什么时候显示 typedef enum { UITextFieldViewModeNever, 从不出现 UITextFieldViewModeWhileEditing, 编辑时出现 UITextFieldViewModeUnlessEditing, 除了编辑外都出现 UITextFieldViewModeAlways 一直出现 } UITextFieldViewMode; textField.clearButtonMode=UITextFieldViewModeWhileEditing; 7.输入框中初始的文字 textField.text=@"textField"; 8.输入的字符就变成点 用于密码输入

textField.secureTextEntry =YES; 9.是否自动纠错

typedef enum { UITextAutocorrectionTypeDefault, 默认 UITextAutocorrectionTypeNo, 不自动纠错 UITextAutocorrectionTypeYes, 自动纠错 } UITextAutocorrectionType;

textField.clearButtonMode=UITextFieldViewModeWhileEditing;

10.内容对齐方式

typedef NS_ENUM(NSInteger, NSTextAlignment) { NSTextAlignmentLeft = 0, // Visually left aligned

if TARGET_OS_IPHONE

NSTextAlignmentCenter    = 1,    // Visually centered 
NSTextAlignmentRight     = 2,    // Visually right aligned 

else / !TARGET_OS_IPHONE /

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 

}
textField.textAlignment=NSTextAlignmentCenter; 11.内容的垂直对齐方式

typedef NS_ENUM(NSInteger, UIControlContentVerticalAlignment) { UIControlContentVerticalAlignmentCenter = 0, UIControlContentVerticalAlignmentTop = 1, UIControlContentVerticalAlignmentBottom = 2, UIControlContentVerticalAlignmentFill = 3, };

textField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter; 12.设置键盘的样式

typedef enum { UIKeyboardTypeDefault, 默认键盘,支持所有字符 UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘 UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符 UIKeyboardTypeURL, URL键盘,支持.com按钮 只支持URL字符 UIKeyboardTypeNumberPad, 数字键盘 UIKeyboardTypePhonePad, 电话键盘 UIKeyboardTypeNamePhonePad, 电话键盘,也支持输入人名 UIKeyboardTypeEmailAddress, 用于输入电子 邮件地址的键盘 UIKeyboardTypeDecimalPad, 数字键盘 有数字和小数点 UIKeyboardTypeTwitter, 优化的键盘,方便输入@、#字符 UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, } UIKeyboardType;

textField.keyboardType=UIKeyboardTypeNumberPad; 13.设置首字母是否自动大写

textField.autocapitalizationType=UITextAutocapitalizationTypeWords; typedef enum { UITextAutocapitalizationTypeNone, 不自动大写 UITextAutocapitalizationTypeWords, 单词首字母大写 UITextAutocapitalizationTypeSentences, 句子的首字母大写 UITextAutocapitalizationTypeAllCharacters, 所有字母都大写 } UITextAutocapitalizationType; 14.return键变成什么键

text.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; 15.键盘外观 typedef enum { UIKeyboardAppearanceDefault, 默认外观,浅灰色 UIKeyboardAppearanceAlert, 深灰 石墨色 } UIReturnKeyType;

textField.keyboardAppearance=UIKeyboardAppearanceAlert;

  1. 设置右侧的view

typedef enum { UITextFieldViewModeNever, UITextFieldViewModeWhileEditing, UITextFieldViewModeUnlessEditing, UITextFieldViewModeAlways } UITextFieldViewMode;

UIImageView *imageView=[[[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"button_up"]]autorelease]; 
textField.rightView=imageView; 
textField.rightViewMode=UITextFieldViewModeAlways; 

17.设置leftView

@property(nonatomic,retain) UIView leftView; // e.g. magnifying glass @property(nonatomic) UITextFieldViewMode leftViewMode; // sets when the left view shows up. default is UITextFieldViewModeNever textField.leftView=imageView; textField.leftViewMode=UITextFieldViewModeAlways; 18.UITextField 的背景, 注意只有UITextBorderStyleNone的时候改属性有效 UIImage image=[UIImageimageNamed:@"button_up"]; textField.background=image;

@property(nonatomic,retain) UIImage background; // default is nil. draw in border rect. image should be stretchable @property(nonatomic,retain) UIImage disabledBackground; // default is nil. ignored if background not set. image should be stretchable

  1. 默认值placeholder textField.placeholder=@"hello";
  2. 界面重写绘制行为 除了UITextField对象的风格选项,你还可以定制化UITextField对象,为他添加许多不同的重写方法,来改变文本字段的显示行为。这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围。以下方法都可以重写。

– textRectForBounds:    //重写来重置文字区域 – drawTextInRect:    //改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了. – placeholderRectForBounds:  //重写来重置占位符区域 – drawPlaceholderInRect:  //重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了. – borderRectForBounds:  //重写来重置边缘区域 – editingRectForBounds:  //重写来重置编辑区域 – clearButtonRectForBounds:  //重写来重置clearButton位置,改变size可能导致button的图片失真 – leftViewRectForBounds: – rightViewRectForBounds: 其他属性的设置

text.adjustsFontSizeToFitWidth = YES; //设置为YES时文本会自动缩小以适应文本窗口大小. text.minimumFontSize = 20; //设置自动缩小显示的最小字体大小

收藏
1
sina weixin mail 回到顶部