足球游戏_中国足彩网¥体育资讯$

ios中uitextfield 使用全面解析
来源:易贤网 阅读:610 次 日期:2014-12-12 16:12:41
温馨提示:易贤网小编为您整理了“ios中uitextfield 使用全面解析”,方便广大网友查阅!

//初始化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; 

} 

通知

uitextfield派生自uicontrol,所以uicontrol类中的通知系统在文本字段中也可以使用。除了uicontrol类的标准事件,你还可以使用下列uitextfield类特有的事件

uitextfieldtextdidbegineditingnotification

uitextfieldtextdidchangenotification

uitextfieldtextdidendeditingnotification

当文本字段退出编辑模式时触发。通知的object属性存储了最终文本。

因为文本字段要使用键盘输入文字,所以下面这些事件发生时,也会发送动作通知

uikeyboardwillshownotification  //键盘显示之前发送

uikeyboarddidshownotification  //键盘显示之后发送

uikeyboardwillhidenotification  //键盘隐藏之前发送

uikeyboarddidhidenotification  //键盘隐藏之后发送

名单

1、text :设置文本框的默认文本。

2、placeholder : 可以在文本框中显示灰色的字,用于提示用户应该在这个文本框输入什么内容。当这个文本框中输入了数据时,用于提示的灰色的字将会自动消失。

3、background :

4、disabled : 若选中此项,用户将不能更改文本框内容。

5、接下来是三个按钮,用来设置对齐方式。

6、border style : 选择边界风格。

7、clear button : 这是一个下拉菜单,你可以选择清除按钮什么时候出现,所谓清除按钮就是出一个现在文本框右边的小 x ,你可以有以下选择:

7.1 never appears : 从不出现

7.2 appears while editing : 编辑时出现

7.3 appears unless editing :

7.4 is always visible : 总是可见

8、clear when editing begins : 若选中此项,则当开始编辑这个文本框时,文本框中之前的内容会被清除掉。比如,你现在这个文本框 a 中输入了 what ,之后去编辑文本框 b,若再回来编辑文本框 a ,则其中的 what 会被立即清除。

9、text color : 设置文本框中文本的颜色。

10、font : 设置文本的字体与字号。

11、min font size : 设置文本框可以显示的最小字体(不过我感觉没什么用)

12、adjust to fit : 指定当文本框尺寸减小时,文本框中的文本是否也要缩小。选择它,可以使得全部文本都可见,即使文本很长。但是这个选项要跟 min font size 配合使用,文本再缩小,也不会小于设定的 min font size 。

接下来的部分用于设置键盘如何显示。

13、captitalization : 设置大写。下拉菜单中有四个选项:

13.1 none : 不设置大写

13.2 words : 每个单词首字母大写,这里的单词指的是以空格分开的字符串

13.3 sentances : 每个句子的第一个字母大写,这里的句子是以句号加空格分开的字符串

13.4 all characters : 所以字母大写

14、correction : 检查拼写,默认是 yes 。

15、keyboard : 选择键盘类型,比如全数字、字母和数字等。

16、appearance:

17、return key : 选择返回键,可以选择 search 、 return 、 done 等。

18、auto-enable return key : 如选择此项,则只有至少在文本框输入一个字符后键盘的返回键才有效。

19、secure : 当你的文本框用作密码输入框时,可以选择这个选项,此时,字符显示为星号。

名单

1.alignment horizontal 水平对齐方式

2.alignment vertical 垂直对齐方式

3.用于返回一个bool值 输入框是否 selected(选中) enabled(可用) highlighted(高亮)

限制只能输入特定的字符

(bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string{

nscharacterset *cs;

cs = [[nscharacterset charactersetwithcharactersinstring:numbers]invertedset];

nsstring *filtered = [[string componentsseparatedbycharactersinset:cs]componentsjoinedbystring:@]; //按cs分离出数组,数组按@分离出字符串

bool canchange = [string isequaltostring:filtered];

return canchange;

}

上面那个numbers是一个宏,可以在文件顶部定义:

#define numbers @”0123456789n” (这个代表可以输入数字和换行,请注意这个n,如果不写这个,done按键将不会触发,如果用在searchbar中,将会不触发search事件,因为你自己限制不让输入n,好惨,我在项目中才发现的。)

所以,如果你要限制输入英文和数字的话,就可以把这个定义为:

#define kalphanum @”abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789″。

当然,你还可以在以上方法return之前,做一提示的,比如提示用户只能输入数字之类的。如果你觉得有需要的话。

限制只能输入一定长度的字符

- (bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string;

{ //string就是此时输入的那个字符 textfield就是此时正在输入的那个输入框 返回yes就是可以改变输入框的值 no相反

if ([string isequaltostring:@n]) //按会车可以改变

{

return yes;

}

nsstring * tobestring = [textfield.text stringbyreplacingcharactersinrange:range withstring:string]; //得到输入框的内容

if (self.mytextfield == textfield) //判断是否时我们想要限定的那个输入框

{

if ([tobestring length] > 20) { //如果输入框内容大于20则弹出警告

textfield.text = [tobestring substringtoindex:20];

uialertview *alert = [[[uialertview alloc] initwithtitle:nil message:@超过最大字数不能输入了 delegate:nil cancelbuttontitle:@ok otherbuttontitles:nil, nil] autorelease];

[alert show];

return no;

}

}

return yes;

}

中国足彩网信息请查看IT技术专栏

中国足彩网信息请查看技术文章
易贤网手机网站地址:ios中uitextfield 使用全面解析
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 足球游戏_中国足彩网¥体育资讯$ 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标