IOS UI学习教程之设置UITextField各种属性
程序员文章站
2023-11-23 21:16:58
uitextfield是ios中非常常用的一个控件,用来接收用户输入信息,完成应用和用户的交互。它的主要属性设置如下:
//初始化textfield并设置位置...
uitextfield是ios中非常常用的一个控件,用来接收用户输入信息,完成应用和用户的交互。它的主要属性设置如下:
//初始化textfield并设置位置及大小 uitextfield *text = [[uitextfield alloc]initwithframe:cgrectmake(20, 20, 130, 30)]; //设置边框样式,只有设置了才会显示边框样式 text.borderstyle = uitextborderstyleroundedrect; typedef enum { uitextborderstylenone, uitextborderstyleline, uitextborderstylebezel, uitextborderstyleroundedrect } uitextborderstyle; //设置输入框的背景颜色,此时设置为白色 如果使用了自定义的背景图片边框会被忽略掉 text.backgroundcolor = [uicolor whitecolor]; //设置背景 text.background = [uiimage imagenamed:@"dd.png"]; //设置背景 text.disabledbackground = [uiimage imagenamed:@"cc.png"]; //当输入框没有内容时,水印提示 提示内容为password text.placeholder = @"password"; //设置输入框内容的字体样式和大小 text.font = [uifont fontwithname:@"arial" size:20.0f]; //设置字体颜色 text.textcolor = [uicolor redcolor]; //输入框中是否有个叉号,在什么时候显示,用于一次性删除输入框中的内容 text.clearbuttonmode = uitextfieldviewmodealways; typedef enum { uitextfieldviewmodenever, 重不出现 uitextfieldviewmodewhileediting, 编辑时出现 uitextfieldviewmodeunlessediting, 除了编辑外都出现 uitextfieldviewmodealways 一直出现 } uitextfieldviewmode; //输入框中一开始就有的文字 text.text = @"一开始就在输入框的文字"; //每输入一个字符就变成点 用语密码输入 text.securetextentry = yes; //是否纠错 text.autocorrectiontype = uitextautocorrectiontypeno; typedef enum { uitextautocorrectiontypedefault, 默认 uitextautocorrectiontypeno, 不自动纠错 uitextautocorrectiontypeyes, 自动纠错 } uitextautocorrectiontype; //再次编辑就清空 text.clearsonbeginediting = yes; //内容对齐方式 text.textalignment = uitextalignmentleft; //内容的垂直对齐方式 uitextfield继承自uicontrol,此类中有一个属性contentverticalalignment text.contentverticalalignment = uicontrolcontentverticalalignmentcenter; //设置为yes时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动 textfied.adjustsfontsizetofitwidth = yes; //设置自动缩小显示的最小字体大小 text.minimumfontsize = 20; //设置键盘的样式 text.keyboardtype = uikeyboardtypenumberpad; typedef enum { uikeyboardtypedefault, 默认键盘,支持所有字符 uikeyboardtypeasciicapable, 支持ascii的默认键盘 uikeyboardtypenumbersandpunctuation, 标准电话键盘,支持+*#字符 uikeyboardtypeurl, url键盘,支持.com按钮 只支持url字符 uikeyboardtypenumberpad, 数字键盘 uikeyboardtypephonepad, 电话键盘 uikeyboardtypenamephonepad, 电话键盘,也支持输入人名 uikeyboardtypeemailaddress, 用于输入电子 邮件地址的键盘 uikeyboardtypedecimalpad, 数字键盘 有数字和小数点 uikeyboardtypetwitter, 优化的键盘,方便输入@、#字符 uikeyboardtypealphabet = uikeyboardtypeasciicapable, } uikeyboardtype; //首字母是否大写 text.autocapitalizationtype = uitextautocapitalizationtypenone; typedef enum { uitextautocapitalizationtypenone, 不自动大写 uitextautocapitalizationtypewords, 单词首字母大写 uitextautocapitalizationtypesentences, 句子的首字母大写 uitextautocapitalizationtypeallcharacters, 所有字母都大写 } uitextautocapitalizationtype; //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; //键盘外观 textview.keyboardappearance=uikeyboardappearancedefault; typedef enum { uikeyboardappearancedefault, 默认外观,浅灰色 uikeyboardappearancealert, 深灰 石墨色 } uireturnkeytype; //设置代理 用于实现协议 text.delegate = self; //把textfield加到视图中 [self.window addsubview:text]; //最右侧加图片是以下代码 左侧类似 uiimageview *image=[[uiimageview alloc] initwithimage:[uiimage imagenamed:@"right.png"]]; text.rightview=image; text.rightviewmode = uitextfieldviewmodealways; typedef enum { uitextfieldviewmodenever, uitextfieldviewmodewhileediting, uitextfieldviewmodeunlessediting, uitextfieldviewmodealways } uitextfieldviewmode; //按return键键盘往下收 becomefirstresponder 类要采用uitextfielddelegate协议 text.delegate = self; 声明text的代理是我,我会去实现把键盘往下收的方法 这个方法在uitextfielddelegate里所以我们要采用uitextfielddelegate这个协议 - (bool)textfieldshouldreturn:(uitextfield *)textfield { [text resignfirstresponder]; //主要是[receiver resignfirstresponder]在哪调用就能把receiver对应的键盘往下收 return yes; } 重写绘制行为 除了uitextfield对象的风格选项,你还可以定制化uitextfield对象,为他添加许多不同的重写方法,来改变文本字段的显示行为。这些方法都会返回一个cgrect结构,制定了文本字段每个部件的边界范围。以下方法都可以重写。 – textrectforbounds: //重写来重置文字区域 – drawtextinrect: //改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了. – placeholderrectforbounds: //重写来重置占位符区域 – drawplaceholderinrect: //重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了. – borderrectforbounds: //重写来重置边缘区域 – editingrectforbounds: //重写来重置编辑区域 – clearbuttonrectforbounds: //重写来重置clearbutton位置,改变size可能导致button的图片失真 – leftviewrectforbounds: – rightviewrectforbounds: 委托方法 - (bool)textfieldshouldbeginediting:(uitextfield *)textfield{ //返回一个bool值,指定是否循序文本字段开始编辑 return yes; } - (void)textfielddidbeginediting:(uitextfield *)textfield{ //开始编辑时触发,文本字段将成为first responder } - (bool)textfieldshouldendediting:(uitextfield *)textfield{ //返回bool值,指定是否允许文本字段结束编辑,当编辑结束,文本字段会让出first responder //要想在用户结束编辑时阻止文本字段消失,可以返回no //这对一些文本字段必须始终保持活跃状态的程序很有用,比如即时消息 return no; } - (bool)textfield:(uitextfield*)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string{ //当用户使用自动更正功能,把输入的文字修改为推荐的文字时,就会调用这个方法。 //这对于想要加入撤销选项的应用程序特别有用 //可以跟踪字段内所做的最后一次修改,也可以对所有编辑做日志记录,用作审计用途。 //要防止文字被改变可以返回no //这个方法的参数中有一个nsrange对象,指明了被改变文字的位置,建议修改的文本也在其中 return yes; } - (bool)textfieldshouldclear:(uitextfield *)textfield{ //返回一个bool值指明是否允许根据用户请求清除内容 //可以设置在特定条件下才允许清除内容 return yes; } -(bool)textfieldshouldreturn:(uitextfield *)textfield{ //返回一个bool值,指明是否允许在按下回车键时结束编辑 //如果允许要调用resignfirstresponder 方法,这回导致结束编辑,而键盘会被收起[textfield resignfirstresponder]; //查一下resign这个单词的意思就明白这个方法了 return yes; }
以上就是本文的全部内容,希望对大家学习ios程序设计有所帮助。
下一篇: Android启动相机拍照并返回图片