UIkit框架之UItextfield

1.继承链:UIcontrol:UIview:UIresponder:NSObject

2.成为第一响应者:[text becomeFirstResponder];  //让该文本成为第一响应者

3.使用resignFirstResponder方法可以取消键盘

4.想要显示键盘需要遵守UITextInputTraits协议,遵守这个协议可以获得以下的行为:

      (1)显示键盘的类型,比如英文的、中文的、数字的

      (2)键盘的自动大小写行为

      (3)键盘的自动纠正行为

      (4)return类型的显示

5.键盘响应的通知机制:

6.为文本添加按钮:

    text.rightView = button; //在文本的右边加上一个按钮

    text.rightViewMode = UITextFieldViewModeAlways; //添加按钮显示的格式

7.显示文本清除功能:

    text.clearButtonMode = UITextFieldViewModeAlways;

    text.clearsOnBeginEditing = YES; //在文本的右边显示清除功能

8.如果想要使用文本的委托功能和管理文本和用户的交互,需要遵守 UITextFieldDelegate协议

9.文本界面属性:

Attribute

Description

Text

The initial text displayed by the text field. You can specify the text as a plain string or as an attributed string. If you specify an attributed string, Interface Builder provides different options for editing the font, color, and formatting of the string.

Color

The color of the text field’s text. To set this attribute programmatically, use the textColor property. 

Font

The font of the text field’s text. To set this attribute programmatically, use the fontproperty.

Alignment

The alignment of the text field’s text inside the editing area. This option is available only when formatting plain strings. To set this attribute programmatically, use the textAlignment property.

Placeholder

The placeholder text displayed by the text field. When the text field’s string is empty, the text field displays this string instead, formatting the string so as to indicate that it is not the actual text. Typing any text into the text field hides this string. To set this attribute programmatically, use the placeholder property.

Background

The background image to display when the text field is enabled. This image is displayed behind the rest of the text field’s content. To set this attribute programmatically, use the background property.

Disabled

The background image to display when the text field is disabled. This image is displayed behind the rest of the text field’s content. To set this attribute programmatically, use the disabledBackground property.

Border Style

The visual style for the text field’s border. This attribute defines the visual border, if any, drawn around the editable text region. To set this attribute programatically, use the borderStyle property.

Clear Button

The behavior of the clear button. The clear button is a built-in overlay view that the user taps to delete all of the text in a text field. Use this attribute to define when the clear button appears. To set this attribute programatically, use theclearButtonModeproperty.

Min Font Size

The minimum font size for the text field’s text. When the Adjust to Fit option is enabled, the text field automatically varies the font size to ensure maximum legibility of the text. You can use this attribute to specify the smallest font size that your consider appropriate for your text. To set this attribute programatically, use theminimumFontSize property.

10.键盘界面属性:想了解这些功能的可以看UITextInputTraits协议

Attribute

Description

Capitalization

The automatic capitalization style to apply to typed text. This attribute determines at what time the Shift key is automatically pressed. You can access the value of this attribute programmatically using the text field’s autocapitalizationTypeproperty. 

Correction

The autocorrection behavior of the text field. This attribute determines whether autocorrection is enabled or disabled during typing. You can access the value of this attribute programmatically using the text field’s autocorrectionType property.

Spell Checking

The spell checking behavior of the text field. This attribute determines whether spell checking is enabled or disabled during typing. You can access the value of this attribute programmatically using the text field’s spellCheckingType property.

Keyboard Type

The style of the text field’s keyboard. This property specifies the type of keyboard displayed when the text field is active. You can access the value of this attribute programmatically using the text field’s keyboardType property.

Appearance

The visual style applied to the text field’s keyboard. Use this property to specify a dark or light keyboard. You can access the value of this attribute programmatically using the text field’s keyboardAppearance property. 

Return Key

The type of return key to display on the keyboard. Use this property to configure the text and visual style applied to the keyboard’s return key. You can access the value of this attribute programmatically using the text field’s returnKeyTypeproperty.

The return key is disabled by default and becomes enabled only when the user types some text into the text field. To respond to taps in the Return key, implement the textFieldShouldReturn: method in the delegate you assign to the text field. 

11.可存取的属性:

    (1)text

    (2)attributedText

    (3)placeholder

    (4)attributedPlaceHolder

    (5)defaultTextAttributes:应用于文本的默认属性

    (6)font

    (7)textColor

    (8)textAlignment

    (9)typingAttribute:提供一个含有文本属性的字典,当用户改变文本的时候就改变文本属性

    

12.管理文本框文本

    (1)adjustsFontSizeToFitWidth:布尔类型,是否让文本的大小适应文本的宽度

    (2)minimumFontSize:cgfloat类型,文本框可允许的最小文字大小,,默认值是0

13.管理编辑行为

    (1)editing:布尔类型,确认文本当前是否正在编辑状态

    (2)clearOnBeginEditing:布尔类型,在重新进行文本编辑的时候是否清除旧的文本

    (3)clearsOnInsertion:布尔类型,在插入文本的时候是否代替之前的内容

    (4)allowsEditiingTextAttributes:布尔类型,是否允许用户在文本框内编辑文本的属性

14.设置视图背景显示

    (1)borderStype: UITextBorderStyle类型,设置边框的风格

    (2)background:UIimage类型,设置背景图片

    (3)disabledBackground:UIimage类型,当文本不使用的时候就这个来当背景。

    (4)clearButtonMode:UITextFieldViewMode类型 ,控制在文本框的标准清除按钮控件

    (5)leftView:UIView类型,用一个视图来覆盖文本框左边的区域,和rightview的功能类型

    (6)leftViewMode:UITextFieldViewMode类型,控制什么时候左边的覆盖视图出现在文本框

 15.可存取的委托:

    (1)delegateidUITextFieldDelegate >类型,可以用来响应一些用户输入的命令或者信息,例如return被按下的时候

16.重载绘制和位置功能:

    (1)- (CGRect)textRectForBounds:(CGRect)bounds:这个方法一般都不会直接使用,返回当你想自定义文本框的rect属性的时通过重载的方法使用。

    (2)- (void)drawTextInRect:(CGRect)rect :这个方法一般是用来进行重载的时候来重新定义你想要的文本的rect属性

    (3)- (CGRect)placeholderRectForBounds:(CGRect)bounds   : 这个方法一般用来进行重载的时候返回重新定义你想要背景文字的rect属性

    (4)- (void)drawPlaceholderInRect:(CGRect)rect :绘制背景文字的显示矩形大小

    (5)- (CGRect)borderRectForBounds:(CGRect)bounds  : 返回接收者的边框矩形

    (6)- (CGRect)editingRectForBounds:(CGRect)bounds   :返回能够被编辑区域的矩形

    (7)- (CGRect)clearButtonRectForBounds:(CGRect)bounds  :返回内置清除按钮的绘制矩形,当你想要改变按钮的位置的时候可以重载这个方法

    (8)- (CGRect)leftViewRectForBounds:(CGRect)bounds    : 返回左边覆盖视图的矩形 ,当你想改变视图的位置的时候可以重载这个方法进行调整

17.代替系统的输入视图:

      (1)inputView :当文本成为第一响应者的时候就会显示这个视图,为nil的时候会显示系统的键盘

      (2)inputAccessoryView :附加视图,可以为键盘添加附加的工具栏

18.功能辅助:

    (1)UITextBorderStyle的类型:

        UITextBorderStyleNone:不显示边界

        UITextBorderStyleLine:显示一条线作为边界

        UITextBorderStyleBezel: 通常应用到数据输入的文本框

        UITextBorderStyleRoundedRect:显示圆角矩形的边界

    (2)UITextFieldViewMode的类型:

        UITextFieldViewModeNever:覆盖视图从来不显示

        UITextFieldViewModeWhileEditing:只有当文本框正在编辑的时候才会显示这个覆盖视图

        UITextFieldViewModeUnlessEditing:只有当文本框不编辑的时候才会显示这个覆盖视图

        UITextFieldViewModeAlways:无论什么时候都会显示这个覆盖视图

    (3)UITextFieldTextDidBeginEditingNotification:通知观察者文本已经开始编辑

    (4)UITextFieldTextDidChangeNotification:通知观察者文本的内容已经改变

    (5)UITextFieldTextDidEndEditingNotification:通知观察着文本的内容已经编辑结束

原文地址:https://www.cnblogs.com/lelun/p/5680763.html