UITextField详解

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.内容对齐方式

typedefNS_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.内容的垂直对齐方式 

typedefNS_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;

16. 设置右侧的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

19. 默认值placeholder

    textField.placeholder=@"hello";

20. 界面重写绘制行为

除了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; //设置自动缩小显示的最小字体大小

UITextFieldDelegate 方法详解:

第一继承自UIControl

// 设置输入框,是否可以被修改

// NO-将无法修改,不出现键盘

// YES-可以修改,默认值

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

return YES;


}

// 当点击键盘的返回键(右下角)时,执行该方法。

// 一般用来隐藏键盘

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

if (txtAccount == textField) {

[txtAccount resignFirstResponder];

}

return YES;

}

// 当输入框获得焦点时,执行该方法。

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

NSLog(@"textFieldDidBeginEditing");


}

// 指定是否允许文本字段结束编辑,允许的话,文本字段会失去first responder

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

return YES;


}

// 文本框失去first responder 时,执行

- (void)textFieldDidEndEditing:(UITextField *)textField{

NSLog(@"textFieldDidEndEditing");


}



// 指明是否允许根据用户请求清除内容

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

NSLog(@"textFieldDidEndEditing");

return YES;

}

// 文本框的文本,是否能被修改

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

return YES;


}

 



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值