您的位置:首页 > 产品设计 > UI/UE

UITextField常用属性和方法详解

2016-04-29 16:11 543 查看
一、属性

[align=left]text: default is nil。显示到 textField 上面的文本。[/align]

[align=left]attributedText: default is nil。显示到 textField 上面的文本属性(NSAttributedString类型,其中包括:字体,颜色等属性)。[/align]

[align=left]textColor: default is nil. use opaque black。文本颜色,默认是黑色。[/align]

[align=left]font: default is nil. use system font 12 pt。文本字体,默认12号字。[/align]

[align=left]textAlignment: default is NSLeftTextAlignment。文本对齐方式,默认左对齐。[/align]

[align=left]borderStyle: default is UITextBorderStyleNone. If set to UITextBorderStyleRoundedRect, custom background images are ignored.textField 的边框样式,默认是没有边框,如果自定义是圆角边框,自定义背景图片设置不成功。[/align]

[align=left]defaultTextAttributes: applies attributes to the full range of text. Unset attributes act like default values.设置 TextField的默认文本属性(字体,大小,颜色等)[/align]

@property(nonatomic,copy) NSDictionary<NSString *, id> *defaultTextAttributes;

复制代码

[align=left]placeholder: default is nil. string is drawn 70% gray。默认 nil,颜色为70%灰色。在该文本字段当中没有其他文本显示时的占位提示字符串。[/align]

[align=left]attributedPlaceholder: default is nil。在该文本字段当中没有其他文本显示时的占位提示属性字符串(NSAttributedString类型,其中包括:字体,颜色等属性)。[/align]

[align=left]clearsOnBeginEditing: default is NO which moves cursor to location clicked. if YES, all text cleared。默认没有光标移到,如果设置 YES,出现光标清空所有文本内容[/align]

[align=left]adjustsFontSizeToFitWidth: default is NO. if YES, text will shrink to minFontSize along baseline.设置为YES时,文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动。[/align]

[align=left]minimumFontSize: default is 0.0. actual min may be pinned to something readable. used if adjustsFontSizeToFitWidth is YES.默认是0.0。如果adjustsFontSizeToFitWidth设置 YES 时,会自动调整最小文本字体。[/align]

[align=left]delegate: default is nil. weak reference。代理属性,默认nil,弱引用。[/align]

@property(nullable, nonatomic,weak) id<UITextFieldDelegate> delegate;

复制代码

[align=left]background: default is nil. draw in border rect. image should be stretchable。设置背景图片。[/align]

@property(nullable, nonatomic,strong) UIImage *background;

复制代码

[align=left]disabledBackground:default is nil. ignored if background not set. image should be stretchable。(默认 nil,忽略背景不设置。图片可能会被拉伸.)当UITextField被禁用时,显示文本的背景图片。[/align]

[align=left]editing: 一个布尔值,表示文本字段当前处于编辑模式中(只读)[/align]

[align=left]allowsEditingTextAttributes: default is NO. allows editing text attributes with style operations and pasting rich text(默认是 NO,是否允许更改字符属性字典)[/align]

[align=left]typingAttributes: automatically resets when the selection changes.(当选中文本时,自动复位)适用于用户输入的新文本的属性。字典包含适用于新类型文本的属性键(和对应的值)。当文本字段的选择更改时,自动清除字典的内容。如果文本字段不是编辑模式,此属性包含值为零。类似地,除非文本字段当前处于编辑模式,否则不能将此属性赋值给该属性。???????????????[/align]

@property(nullable, nonatomic,copy) NSDictionary<NSString *, id> *typingAttributes

复制代码

[align=left]clearButtonMode: sets when the clear button shows up. default is UITextFieldViewModeNever.清除文本按钮,默认不显示。[/align]

[align=left]leftView: e.g. magnifying glass。左侧 view 视图,例如搜索栏当中的放大镜。要配合 leftViewMode 使用。[/align]

[align=left]leftViewMode: sets when the left view shows up. default is UITextFieldViewModeNever。当设置左侧视图后,默认模式是UITextFieldViewModeNever。[/align]

[align=left]rightView: e.g. bookmarks button.右侧 view,例如书签按钮。要配合 rightViewMode 使用。[/align]

[align=left]rightViewMode: sets when the right view shows up. default is UITextFieldViewModeNever当设置右侧视图后,默认模式是UITextFieldViewModeNever。[/align]

inputView::
当文本字段成为第一响应者时,自定义输入视图显示。

inputAccessoryView::
当文本字段成为第一响应者时,该自定义辅助视图显示。

[align=left]clearsOnInsertion:布尔值,该值设置是否允许再次编辑时在内容中间插入文本替换先前内容。默认 NO。[/align]

二、方法

当需要重新布局
UITextField 内部子控件位置时,重写下面方法:

[align=left]borderRectForBounds:: 线条frame[/align]

- (CGRect)borderRectForBounds: (CGRect)bounds;

[align=left]textRectForBounds:: 文本 frame[/align]

- (CGRect)textRectForBounds: (CGRect)bounds;

[align=left]placeholderRectForBounds:: 占位文本 frame[/align]

- (CGRect)placeholderRectForBounds: (CGRect)bounds;

[align=left]editingRectForBounds:: 编辑时文本的 frame[/align]

- (CGRect)editingRectForBounds: (CGRect)bounds;

[align=left]clearButtonRectForBounds:: 清空文本按钮的 frame[/align]

- (CGRect)clearButtonRectForBounds: (CGRect)bounds;

[align=left]leftViewRectForBounds:: 左侧视图的 frame[/align]

- (CGRect)leftViewRectForBounds: (CGRect)bounds;

[align=left]rightViewRectForBounds:: 右侧视图的 frame[/align]

- (CGRect)rightViewRectForBounds: (CGRect)bounds;

[align=left]drawTextInRect:: 绘制指定矩形中的文本。 此方法不应该直接调用。如果你想自定义文本的绘图行为,可以重写此方法来进行绘图。按此方法被调用时,当前图形上下文已被配置为图形的默认环境和文本颜色。在方法中,你可以设置当前上下文,然后调用父类去实现绘图或者你可以自己进行绘图。如果你自己渲染,你就不应该调用父类。[/align]

- (void)drawTextInRect: (CGRect)rect;

[align=left]drawPlaceholderInRect:: 绘制占位符文本在指定的矩形上。 此方法不应该直接调用。如果你想自定义占位符文本的绘图行为,可以重写此方法来进行绘图。按此方法被调用时,当前图形上下文已被配置为图形的默认环境和文本颜色。在方法中,你可以设置当前上下文进,然后调用父类去实现绘图或者你可以自己进行绘图。如果你自己渲染,你就不应该调用父类。[/align]

- (void)drawPlaceholderInRect: (CGRect)rect;

UIView (UITextField)方法

[align=left]endEditing:: use to make the view or any subview that is the first responder resign (optionally force).辞掉第一响应者。[/align]

- (BOOL)endEditing: (BOOL)force;

三、UITextFieldDelegate代理方法

[align=left]textFieldShouldBeginEditing:: return NO to disallow editing.当文本将要开始编辑时调用这个代理方法,返回 NO 时驳回编辑状态。[/align]

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

[align=left]textFieldDidBeginEditing:: became first responder。当文本正在开始编辑时调用,变为第一响应者。[/align]

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

[align=left]textFieldShouldEndEditing:: return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end。当文本将要结束编辑时进行调用,返回 YES 时允许编辑停止或者注销第一响应者,返回 NO,不允许编辑会话结束。[/align]
[align=left](BOOL)textFieldShouldEndEditing: (UITextField *)textField;[/align]

[align=left]textFieldDidEndEditing:: may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called。文本标签正在结束编辑。[/align]

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

[align=left]textField:shouldChangeCharactersInRange:replacementString:: return NO to not change text。询问代理文本是否可以被替换,如果返回 NO,不被替换,YES 可被替换。[/align]

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

[align=left]textFieldShouldClear:: called when clear button pressed. return NO to ignore (no notifications)。文本将要被清空时调用,返回 NO,文本清空将被忽略。[/align]

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

[align=left]textFieldShouldReturn:: called when 'return' key pressed. return NO to ignore.按 return 键的时候调用此方法。当按 return 时,返回 NO 会忽略。[/align]

- (BOOL)textFieldShouldReturn: (UITextField *)textField;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: