UITextField 输入框

来源:互联网 发布:linux怎么看硬盘空间 编辑:程序博客网 时间:2024/05/16 13:47
概述:
    UITextField提供了一个可被编辑的label, 与相应的输入等一些列功能. 对于UITextField的使用, 主要由对其但输入框视图外观的设置, 和键盘的设置.

1. 输入框相关属性

文本属性
@property(nonatomic, copy) NSString *text
@property(nonatomic, copy) NSAttributedString *attributedText
@property(nonatomic, copy) NSString *placeholder
@property(nonatomic, copy) NSAttributedString *attributedPlaceholder
@property(nonatomic, copy) NSDictionary *defaultTextAttributes
@property(nonatomic, retain) UIFont *font
@property(nonatomic, retain) UIColor *textColor
@property(nonatomic) NSTextAlignment textAlignment
@property(nonatomic, copy) NSDictionary *typingAttributes

文本
带属性的文本
预留文本
带属性的预留文本
默认文本属性
字体大小
字体颜色
文本对齐方式
设置新的文本属性(编辑状态有效)
自适应文本框
@property(nonatomic) BOOL adjustsFontSizeToFitWidth
@property(nonatomic) CGFloat minimumFontSize

字体大小自适应文本宽度
最小字体
管理编辑行为
@property(nonatomic, readonly, getter=isEditing) BOOL editing
@property(nonatomic) BOOL clearsOnBeginEditing
@property(nonatomic) BOOL clearsOnInsertion

编辑状态(只读)
在编辑时清空内容
插入新内容时, 清空内容
设置视图背景外观
@property(nonatomic) UITextBorderStyle borderStyle
@property(nonatomic, retain) UIImage *background
@property(nonatomic, retain) UIImage *disabledBackground

输入框类型
背景图片(有背景图片时, 输入框类型将会失效)
无效状态下的背景图片
辅助视图管理
@property(nonatomic) UITextFieldViewMode clearButtonMode
@property(nonatomic, retain) UIView *leftView
@property(nonatomic) UITextFieldViewMode leftViewMode
@property(nonatomic, retain) UIView *rightView
@property(nonatomic) UITextFieldViewMode rightViewMode

清空按钮模式
左侧辅助视图
左侧辅助视图模式
右侧辅助视图
右侧辅助视图模式
代理
@property(nonatomic, assign) id< UITextFieldDelegate > delegate
 
重绘

 
输入视图
@property(readwrite, retain) UIView *inputView
@property(readwrite, retain) UIView *inputAccessoryView

输入视图
辅助视图
通知中心

UITextFieldTextDidBeginEditingNotification
UITextFieldTextDidChangeNotification
UITextFieldTextDidEndEditingNotification

键盘的通知消息
因为文本字段要使用键盘输入文字,所以下面这些事件发生时,也会发送动作通知
UIKeyboardWillShowNotification //键盘显示之前发送
UIKeyboardDidShowNotification   //键盘显示之后发送
UIKeyboardWillHideNotification  //键盘隐藏之前发送
UIKeyboardDidHideNotification   //键盘隐藏之后发送
 

键盘类型

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

返回键类型

    UIReturnKeyDefault, 默认 灰色按钮,标有Return
    UIReturnKeyGo,     标有Go的蓝色按钮
    UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索
    UIReturnKeyJoin,标有Join的蓝色按钮
    UIReturnKeyNext,标有Next的蓝色按钮
    UIReturnKeyRoute,标有Route的蓝色按钮
    UIReturnKeySearch,标有Search的蓝色按钮
    UIReturnKeySend,标有Send的蓝色按钮
    UIReturnKeyYahoo,标有Yahoo的蓝色按钮
    UIReturnKeyYahoo,标有Yahoo的蓝色按钮
    UIReturnKeyEmergencyCall, 紧急呼叫按钮


文本输入特性协议: UITextInputTraits


text.autocapitalizationType
首字母是否大写
UITextAutocapitalizationTypeNone,全不
UITextAutocapitalizationTypeWords,每个单词
UITextAutocapitalizationTypeSentences,每个词
UITextAutocapitalizationTypeAllCharacters,每个句子
text.autocorrectionType
是否纠错
UITextAutocorrectionTypeDefault, 默认
UITextAutocorrectionTypeNo,  不自动纠错
UITextAutocorrectionTypeYes, 自动纠错


2. UITextFieldDelegate协议中的方法

  • - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{  
        //返回一个BOOL值,指定是否循序文本字段开始编辑  
        return YES;  //这个返回值同时也会触发其他相应的操 作 /应该会触发键盘的弹出
    }  

    · - (void)textFieldDidBeginEditing:(UITextField *)textField{
        //开始编辑时触发,文本字段将成为first responder
      / 应该会触发内容的输入
    }

    · - (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
        //返回BOOL值,指定是否允许文本字段结束编辑,当编辑结束,文本字段会让出first responder
        //要想在用户结束编辑时阻止文本字段消失,可以返回NO
        //这对一些文本字段必须始终保持活跃状态的程序很有用,比如即时消息
        return NO;   /可以用开控制 最多输入的字符个数
    }

    · - (BOOL)textFieldShouldClear:(UITextField *)textField{
        //返回一个BOOL值指明是否允许根据用户请求清除内容
        //可以设置在特定条件下才允许清除内容
        return YES;  /清楚按钮的控制
    }

    · -(BOOL)textFieldShouldReturn:(UITextField *)textField{
        //返回一个BOOL值,指明是否允许在按下回车键时结束编辑
        //如果允许要调用resignFirstResponder 方法,这回导致结束编辑,而键盘会被收起
        [textField resignFirstResponder];//查一下resign这个单词的意思就明白这个方法了
        return YES;  //对于返回键的控制
    }

    · - (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
        //当用户使用自动更正功能,把输入的文字修改为推荐的文字时,就会调用这个方法。
        //这对于想要加入撤销选项的应用程序特别有用
        //可以跟踪字段内所做的最后一次修改,也可以对所有编辑做日志记录,用作审计用途。   
        //要防止文字被改变可以返回NO
        //这个方法的参数中有一个NSRange对象,指明了被改变文字的位置,建议修改的文本也在其中
        return YES;
    }

0 0
原创粉丝点击