博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UITextField常用属性和方法详解
阅读量:2339 次
发布时间:2019-05-10

本文共 5537 字,大约阅读时间需要 18 分钟。


一、属性

  • text: default is nil。显示到 textField 上面的文本。
  • attributedText: default is nil。显示到 textField 上面的文本属性(NSAttributedString类型,其中包括:字体,颜色等属性)
  • textColor: default is nil. use opaque black。文本颜色,默认是黑色。
  • font: default is nil. use system font 12 pt。文本字体,
    默认12号字
  • textAlignment: default is NSLeftTextAlignment。文本对齐方式,默认左对齐。
  • borderStyle: default is UITextBorderStyleNone. If set to UITextBorderStyleRoundedRect, custom background images are ignored.textField 的
    边框样式,默认是没有边框,
    如果自定义是圆角边框,自定义背景图片设置不成功。
  • defaultTextAttributes: applies attributes to the full range of text. Unset attributes act like default values.设置 TextField的默认文本属性(字体,大小,颜色等)
    1. @property(nonatomic,copy) NSDictionary<NSString *, id> *defaultTextAttributes;
    复制代码
  • placeholder: default is nil. string is drawn 70% gray。默认 nil,颜色为70%灰色。在该文本字段当中没有其他文本显示时的占位提示字符串。
  • attributedPlaceholder: default is nil。在该文本字段当中没有其他文本显示时的占位提示属性字符串(NSAttributedString类型,其中包括:字体,颜色等属性)
  • clearsOnBeginEditing: default is NO which moves cursor to location clicked. if YES, all text cleared。默认没有光标移到,如果设置 YES,出现光标清空所有文本内容
  • adjustsFontSizeToFitWidth: default is NO. if YES, text will shrink to minFontSize along baseline.设置为YES时,文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动
  • minimumFontSize: default is 0.0. actual min may be pinned to something readable. used if adjustsFontSizeToFitWidth is YES.默认是0.0。如果adjustsFontSizeToFitWidth设置 YES 时,会自动调整最小文本字体。
  • delegate: default is nil. weak reference。代理属性,默认nil,弱引用。
    1. @property(nullable, nonatomic,weak) id<UITextFieldDelegate> delegate;
    复制代码

  • background: default is nil. draw in border rect. image should be stretchable。设置背景图片。
    1. @property(nullable, nonatomic,strong) UIImage  *background;
    复制代码

  • disabledBackground:default is nil. ignored if background not set. image should be stretchable。(默认 nil,忽略背景不设置。图片可能会被拉伸.)当UITextField被禁用时,显示文本的背景图片。
  • editing: 一个布尔值,表示文本字段当前处于编辑模式中(只读)
  • allowsEditingTextAttributes: default is NO. allows editing text attributes with style operations and pasting rich text(默认是 NO,是否允许更改字符属性字典)
  • typingAttributes: automatically resets when the selection changes.(当选中文本时,自动复位)适用于用户输入的新文本的属性。字典包含适用于新类型文本的属性键(和对应的值)。当文本字段的选择更改时,自动清除字典的内容。如果文本字段不是编辑模式,此属性包含值为零。类似地,除非文本字段当前处于编辑模式,否则不能将此属性赋值给该属性。???????????????
    1. @property(nullable, nonatomic,copy) NSDictionary<NSString *, id> *typingAttributes
    复制代码

  • clearButtonMode: sets when the clear button shows up. default is UITextFieldViewModeNever.
    清除文本按钮,默认不显示
  • leftView: e.g. magnifying glass。
    左侧 view 视图
    ,例如搜索栏当中的放大镜。要配合 leftViewMode 使用。
  • leftViewMode: sets when the left view shows up. default is UITextFieldViewModeNever。当设置左侧视图后,默认模式是UITextFieldViewModeNever。
  • rightView: e.g. bookmarks button.
    右侧 view
    ,例如书签按钮。要配合 rightViewMode 使用。
  • rightViewMode: sets when the right view shows up. default is UITextFieldViewModeNever当设置右侧视图后,默认模式是UITextFieldViewModeNever。
  • inputView::
    当文本字段成为第一响应者时,自定义
    输入视图显示。
  • inputAccessoryView::
    当文本字段成为第一响应者时,该自定义辅助视图显示
  • clearsOnInsertion:布尔值,该值设置是否允许再次编辑时在内容中间插入文本替换先前内容。默认 NO


二、方法


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

  • borderRectForBounds:: 线条frame
    1. - (CGRect)borderRectForBounds: (CGRect)bounds;

  • textRectForBounds:: 文本 frame
    1. - (CGRect)textRectForBounds: (CGRect)bounds;

  • placeholderRectForBounds:: 占位文本 frame
    1. - (CGRect)placeholderRectForBounds: (CGRect)bounds;

  • editingRectForBounds:: 编辑时文本的 frame
    1. - (CGRect)editingRectForBounds: (CGRect)bounds;

  • clearButtonRectForBounds:: 清空文本按钮的 frame
    1. - (CGRect)clearButtonRectForBounds: (CGRect)bounds;

  • leftViewRectForBounds:: 左侧视图的 frame
    1. - (CGRect)leftViewRectForBounds: (CGRect)bounds;

  • rightViewRectForBounds:: 右侧视图的 frame
    1. - (CGRect)rightViewRectForBounds: (CGRect)bounds;

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

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


  UIView (UITextField)方法
  • endEditing:: use to make the view or any subview that is the first responder resign (optionally force).辞掉第一响应者。
    1. - (BOOL)endEditing: (BOOL)force;


三、UITextFieldDelegate代理方法
  • textFieldShouldBeginEditing:: return NO to disallow editing.当文本将要开始编辑时调用这个代理方法,返回 NO 时驳回编辑状态。
    1. - (BOOL)textFieldShouldBeginEditing: (UITextField *)textField;

  • textFieldDidBeginEditing:: became first responder。当文本正在开始编辑时调用,变为第一响应者。
    1. - (void)textFieldDidBeginEditing: (UITextField *)textField;

  • textFieldShouldEndEditing:: return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end。当文本将要结束编辑时进行调用,返回 YES 时允许编辑停止或者注销第一响应者,返回 NO,不允许编辑会话结束。
    (BOOL)textFieldShouldEndEditing: (UITextField *)textField;
  • textFieldDidEndEditing:: may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called。文本标签正在结束编辑。
    1. - (void)textFieldDidEndEditing: (UITextField *)textField;

  • textField:shouldChangeCharactersInRange:replacementString:: return NO to not change text。询问代理文本是否可以被替换,如果返回 NO,不被替换,YES 可被替换。
    1. - (BOOL)textField: (UITextField *)textField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string;

  • textFieldShouldClear:: called when clear button pressed. return NO to ignore (no notifications)。文本将要被清空时调用,返回 NO,文本清空将被忽略。
    1. - (BOOL)textFieldShouldClear: (UITextField *)textField;

  • textFieldShouldReturn:: called when 'return' key pressed. return NO to ignore.按 return 键的时候调用此方法。
    当按 return 时,返回 NO 会忽略
    1. - (BOOL)textFieldShouldReturn: (UITextField *)textField;

转载地址:http://ucwvb.baihongyu.com/

你可能感兴趣的文章
Ubuntu修改用户名的问题
查看>>
Copy_from&to_user详解
查看>>
关于bash命令
查看>>
编译内核模块 .ko文件的注意事项 缺少:mmzone.h bounds.h
查看>>
Android开发:检测耳机的插入状态
查看>>
Netty 源码分析-服务端
查看>>
Netty 源码分析-ChannelPipeline
查看>>
分库分表的起源
查看>>
【深入理解JVM虚拟机】第1章 走进java
查看>>
【深入理解JVM虚拟机】第2章 java内存区域与内存溢出异常
查看>>
【深入理解JVM虚拟机】第3章 垃圾收集器与内存分配策略
查看>>
性能优化-jvm
查看>>
性能优化-mysql
查看>>
性能优化-tomcat
查看>>
JVM内存模型、指令重排、内存屏障概念解析
查看>>
【java基础】集合框架总结
查看>>
Elasticsearch-基础介绍及索引原理分析
查看>>
【深入理解JVM虚拟机】第7章 虚拟机类的加载机制
查看>>
【C++】二、指针数组与数组指针
查看>>
【C++】三、const与字符串
查看>>