如何让uitextfielddelegate 具有只读属性 不能在里面编辑

3038人阅读
进入本文之前建议你认真读一读我的另一篇博文:&这样会起到事半功倍效果。
为什么要看另一篇关于UIControl的文章呢?因为UITextField继承自UIControl类,很多UIControl的属性、方法、通知等完全适用于UITextField,而那些属于UIControl的东西我又不会在此赘述。(主要是减轻文章的长度,以最少的文字最通俗易懂讲明白一个知识点是我追求的目标。)
UITextField*&myTextField&=&[[UITextField&alloc]initWithFrame:CGRectMake(50,&100,&200,&50)];&&&
纯代码创建View请参看我的另一片博文:
myTextField.delegate&=&&&
UIControl属性对UITextField完全可以用,下面的都是UITextFiels扩展的属性:
myTextField.textAlignment&=&UITextAlignmentL&&
myTextField.borderStyle&=&UITextBorderStyleB&&
myTextField.placeholder&=&@&请在此输入账号&;&&
myTextField.clearsOnBeginEditing&=&YES;&&
myTextField.adjustsFontSizeToFitWidth&=&YES;&&
myTextField.clearButtonMode&=&UITextFieldViewModeUnlessE&&
这些属性令你可以将UIView的派生类附着于为本字段的左方或右方。人们通常会将UIButton对象,比如放大镜或者书签按钮附着与文本字段上。每个附着视图都会有一个相应的模式,设置clearButtonmode属性的那些值,同样可以设置这个模式。
[self.view&addSubview:myTextField];&&&&
重写绘制行为
除了UITextField对象的风格选项,你还可以定制化UITextField对象,为他添加许多不同的重写方法,来改变文本字段的显示行为。这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围。如果你创见了一个自定义的UITextField类,你可以重写这些方法,这样就可以改变一个或多个边界。一定不要直接调用 fan广发;它们都是被iPhone运行库调用的回调函数下面举个例子:
-&(CGRect)clearButtonForBounds:(CGRect)bounds{&&&&
&&&&return&CGRectMake(bounds.origin.x&+bounds.size.width-50,&&&&&
&&&&&&&&&&&&&&&&&&&&&&bounds.origin.y+bounds.size.height-20,&16,&16);&&&&
下列方法在创建一个UITextField的子类时可以重写:
borderRectForBounds
指定矩形边界
textRectForBounds
&指定显示文本的边界
placeholderRectForBounds
指定站位文本的边界
editingRectForBounds
指定编辑中文本的边界
clearButtonRectForBounds
指定显示清除按钮的边界
leftViewRectForBounds
指定显示左附着视图的边界
rightViewRectForBounds
指定显示右附着视图的边界
-&(BOOL)textFieldShouldBeginEditing:(UITextField&*)textField{&&&&
&&&&return&YES;&&&&
-&(void)textFieldDidBeginEditing:(UITextField&*)textField{&&
-&(BOOL)textFieldShouldEndEditing:(UITextField&*)textField{&&
&&&&return&NO;&&
-&(BOOL)textField:(UITextField*)textField&shouldChangeCharactersInRange:(NSRange)range&replacementString:(NSString&*)string{&&
&&&&return&YES;&&
-&(BOOL)textFieldShouldClear:(UITextField&*)textField{&&
&&&&return&YES;&&
-(BOOL)textFieldShouldReturn:(UITextField&*)textField{&&
&&&&[textField&resignFirstResponder];&&
&&&&return&YES;&&
UITextField派生自UIControl,所以UIControl类中的通知系统在文本字段中也可以使用。除了UIControl类的标准事件,你还可以使用下列UITextField类特有的事件
UITextFieldTextDidBeginEditingNotification
UITextFieldTextDidChangeNotification
UITextFieldTextDidEndEditingNotification
当文本字段退出编辑模式时触发。通知的object属性存储了最终文本。
因为文本字段要使用键盘输入文字,所以下面这些事件发生时,也会发送动作通知
UIKeyboardWillShowNotification
键盘显示之前发送
UIKeyboardDidShowNotification
键盘显示之后发送
UIKeyboardWillHideNotification
键盘隐藏之前发送
UIKeyboardDidHideNotification
键盘隐藏之后发送
打开键盘卷动文本字段
默认情况下打开键盘会遮住下面的view,带来一点点困扰,不过这不是什么大问题,我们使用点小小的手段就可以解决。
首先我们要知道键盘的高度是固定不变的,不过在IOS 5.0 以后键盘的高度貌似不是216了,不过不要紧,我们调整调整就是了:
竖屏(portrait)
横屏(landScape)
我们采取的方法就是在textField(有可能是其他控件)接收到弹出键盘事件时把self.view整体上移216px了(我们就以iPhone竖屏为例了)。
有关View的frame,origin,size之类的知识点不懂的请参看我的另一篇博文:
首先我们要设置textField的代理,我们就设为当前控制器了。
textField,delegate=&&
然后我们在当前控制器实现下面两个委托方法:
-&(void)textFieldDidBeginEditing:(UITextField&*)textField&&
&&&&&&&NSTimeInterval&animationDuration&=&0.30f;&&&&&&
&&&&&&CGRect&frame&=&self.view.&&
&&&&&&frame.origin.y&-=216;&&
&&&&&&frame.size.height&+=216;&&
&&&&&&self.view.frame&=&&&
&&&&&&&[UIView&beginAnimations:@&ResizeView&&context:nil];&&
&&&&&&&[UIView&setAnimationDuration:animationDuration];&&
&&&&&&&self.view.frame&=&&&&&&&&&&&&&&&&&&&
&&&&&&&[UIView&commitAnimations];&&&&&&&&&&&&&&&&&&
-&(BOOL)textFieldShouldReturn:(UITextField&*)textField&&&
&&&&&&&&NSTimeInterval&animationDuration&=&0.30f;&&
&&&&&&&&CGRect&frame&=&self.view.&&&&&&
&&&&&&&&frame.origin.y&+=216;&&&&&&&&
&&&&&&&&frame.size.&height&-=216;&&&&&
&&&&&&&&self.view.frame&=&&&
&&&&[UIView&beginAnimations:@&ResizeView&&context:nil];&&
&&&&[UIView&setAnimationDuration:animationDuration];&&
&&&&&&&&self.view.frame&=&&&&&&&&&&&&&&&&&&&
&&&&&&&&[UIView&commitAnimations];&&
&&&&&&&&[textField&resignFirstResponder];&&&&&
}&&&&&&&&&
-&(void)viewDidLoad&&
&&&&[super&viewDidLoad];&&
&&&&UITextField&*myTextField&=&[[UITextField&alloc]initWithFrame:CGRectMake(50,&300,&200,&60)];&&
&&&&myTextField.delegate&=&&&
&&&&myTextField.contentVerticalAlignment&=&UIControlContentVerticalAlignmentC&&
&&&&myTextField.textAlignment&=&UITextAlignmentL&&
&&&&myTextField.borderStyle&=&UITextBorderStyleB&&
&&&&myTextField.placeholder&=&@&请在此输入账号&;&&
&&&&myTextField.clearsOnBeginEditing&=&YES;&&
&&&&myTextField.adjustsFontSizeToFitWidth&=&YES;&&
&&&&myTextField.clearButtonMode&=&UITextFieldViewModeUnlessE&&
&&&&[self.view&addSubview:myTextField];&&
&&&&myTextField&=&&&
-&(BOOL)textFieldShouldBeginEditing:(UITextField&*)textField{&&
&&&&return&YES;&&
-&(void)textFieldDidBeginEditing:(UITextField&*)textField{&&
&&&&CGRect&frame&=&self.view.&&
&&&&frame.origin.y&-=120;&&
&&&&frame.size.height&+=120;&&
&&&&self.view.frame&=&&&
-&(BOOL)textFieldShouldEndEditing:(UITextField&*)textField{&&
&&&&NSLog(@&here&is&code:&%@&,textField.text);&&
&&&&return&YES;&&
-&(BOOL)textField:(UITextField*)textField&shouldChangeCharactersInRange:(NSRange)range&replacementString:(NSString&*)string{&&
&&&&return&YES;&&
-&(BOOL)textFieldShouldClear:(UITextField&*)textField{&&
&&&&return&YES;&&
-(BOOL)textFieldShouldReturn:(UITextField&*)textField{&&
&&&&CGRect&frame&=&self.view.&&
&&&&frame.origin.y&+=120;&&
&&&&frame.size.height&-=120;&&
&&&&self.view.frame&=&&&
&&&&NSLog(@&textfield:%@&,&textField);&&
&&&&[textField&resignFirstResponder];&&
&&&&return&YES;&&
UITextField 的密文设定方式
通常在输入密码或是制作一些特殊效果时,UITextField 会使用密文来保护所输入的文字,下列我们将使用两种不同的方式来示范如何设定 UITextField 的密文保护。
&一个最简单又直接的方式,当介面上已经拉出一个 UITextField 时,我们可以透过 Interface Builder 直接设定,点选所要的 UITextField 点选属性标籤页 Attributes inspector,并找到 Secure 的 CheckBox 打勾即可,如下图。
&另一种方式,就是从程式里面直接做设定,常用在动态产生 UITextField 时,其程式码如下。
passwordTextField.secureTextEntry&=&YES;&&
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:105136次
积分:1225
积分:1225
排名:第18545名
原创:14篇
转载:41篇
评论:27条
(1)(8)(5)(1)(1)(4)(8)(1)(3)(24)
对学习有用的网址如何让UITextField 具有只读属性 不能在里面编辑_百度知道
如何让UITextField 具有只读属性 不能在里面编辑
提问者采纳
UITextField *[field resignFirstRespond]; 祝你愉快,满意请采纳。
其他类似问题
为您推荐:
uitextfield的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁用心创造滤镜
扫码下载App
汇聚2000万达人的兴趣社区下载即送20张免费照片冲印
扫码下载App
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
阅读(1216)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'UITextField 详解',
blogAbstract:'//&创建',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:4,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'0',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}UITextField使用总结
UITextField使用总结
UITextField* myTextField = [[UITextField
alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];
myTextField.delegate =//委托类需要遵守UITextFieldDelegate协议&
UIControl属性对UITextField完全可以用,下面的都是UITextFiels扩展的属性:
myTextField.textAlignment = UITextAlignmentL//默认就是左对齐,这个是UITextField扩展属性
myTextField.borderStyle = UITextBorderStyleB//默认是没有边框,如果使用了自定义的背景图片边框会被忽略掉
myTextField.placeholder = @"请在此输入账号";//为空白文本字段绘制一个灰色字符串作为占位符
myTextField.clearsOnBeginEditing = YES;//设置为YES当用点触文本字段时,字段内容会被清除
myTextField.adjustsFontSizeToFitWidth = YES;//设置为YES时文本会自动缩小以适应文本窗口大小。默认是保持原来大小,而让长文本滚动
//myTextField.background = [UIImage
imageNamed:@"registBtn"];//可以接受UIImage对象,此项设置则边框失效。
myTextField.clearButtonMode =
UITextFieldViewModeUnlessE//右边显示的'X'清楚按钮
//myTextField.LeftView = &
//myTextField.leftViewMode =&
//myTextField.RightView = &
//myTextField.rightViewMode = &
&&这些属性令你可以将UIView的派生类附着于为本字段的左方或右方。人们通常会将UIButton对象,比如放大镜或者书签按钮附着与文本字段上。每个附着视图都会有一个相应的模式,设置clearButtonmode属性的那些值,同样可以设置这个模式。
[self.view addSubview:myTextField];
- (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
键盘隐藏之后发送
打开键盘卷动文本字段,下面附一个通用的解决键盘遮挡的方法
//用于处理键盘遮挡的问题
- (void)moveView:(UITextField *)textField
leaveView:(BOOL)leave&
& & UIView *accessoryView =
textField.inputAccessoryV&
& & UIView *inputview
textField.inputV&
& & int textFieldY =
& & int accessoryY =
& & if (accessoryView &&
inputview) &
& CGRect accessoryRect =
accessoryView.&
& CGRect inputViewRect =
inputview.&
& accessoryY = 480 - (accessoryRect.size.height +
inputViewRect.size.height);&
& & else if
(accessoryView)&
& CGRect accessoryRect =
accessoryView.&
& accessoryY = 480 - (accessoryRect.size.height +
& & else if
(inputview)&
& CGRect inputViewRect =
inputview.&
& accessoryY = 480
-inputViewRect.size.&
& accessoryY = 264; //480 -
& & CGRect textFieldRect =
textField.&
& & textFieldY =
textFieldRect.origin.y + textFieldRect.size.height +
& & int offsetY = textFieldY -
accessoryY;&
& & if (!leave && offsetY
& int y_offset = -5;&
& y_offset += -offsetY;&
& CGRect viewFrame =
self.view.&
& viewFrame.origin.y +=
& [UIView beginAnimations:nil
context:NULL];&
setAnimationBeginsFromCurrentState:YES];&
setAnimationDuration:0.3];&
& [self.view
setFrame:viewFrame];&
& [UIView commitAnimations];&
& CGRect viewFrame = CGRectMake(0, 20, 320,
& [UIView beginAnimations:nil
context:NULL];&
setAnimationBeginsFromCurrentState:YES];&
setAnimationDuration:0.3];&
& [self.view
setFrame:viewFrame];&
& [UIView commitAnimations];&
使用如下:
- (void)textFieldDidBeginEditing:(UITextField
*)textField&
& & [self moveView:textField
leaveView:NO];&
- (void)textFieldDidEndEditing:(UITextField
& & [self moveView:textField
leaveView:YES];&
下面再补充一些重写绘制相关内容
重写绘制行为
除了UITextField对象的风格选项,你还可以定制化UITextField对象,为他添加许多不同的重写方法,来改变文本字段的显示行为。这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围。如果你创见了一个自定义的UITextField类,你可以重写这些方法,这样就可以改变一个或多个边界。一定不要直接调用
fan广发;它们都是被iPhone运行库调用的回调函数下面举个例子:
- (CGRect)clearButtonForBounds:(CGRect)bounds{
& & return
CGRectMake(bounds.origin.x +bounds.size.width-50,&
bounds.origin.y+bounds.size.height-20, 16, 16);
下列方法在创建一个UITextField的子类时可以重写:
borderRectForBounds
指定矩形边界
textRectForBounds
指定显示文本的边界
placeholderRectForBounds
指定站位文本的边界
editingRectForBounds
指定编辑中文本的边界
clearButtonRectForBounds
指定显示清除按钮的边界
leftViewRectForBounds
指定显示左附着视图的边界
rightViewRectForBounds
指定显示右附着视图的边界
下面附带一个UILabel和UITextField的重绘实现padding效果的代码
首先来看&UILabel&的子类
InsetsLabel&的实现代码:
//1.header file
@interface InsetsLabel : UILabel
@property(nonatomic) UIEdgeI
-(id) initWithFrame:(CGRect)frame andInsets: (UIEdgeInsets)
-(id) initWithInsets: (UIEdgeInsets)
//2. implementation file
#import "InsetsLabel.h"
@implementation InsetsLabel
@synthesize insets=_
-(id) initWithFrame:(CGRect)frame andInsets:(UIEdgeInsets)insets
& & self = [super
initWithFrame:frame];
& & if(self){
& self.insets =
-(id) initWithInsets:(UIEdgeInsets)insets {
& & self = [super init];
& & if(self){
& self.insets =
-(void) drawTextInRect:(CGRect)rect {
& & return [super
drawTextInRect:UIEdgeInsetsInsetRect(rect, self.insets)];
关键就是覆盖了
-(void) drawTextInRect: (CGRect)
&方法,在画&&Label&的文本时分别设置文本与&&Label&四个边的间隙,即画在&Label&内的一个小矩形内,这个例子提供了便利的构造函数,提供自己的
UIEdgeInsets&属性。另外,函数&UIEdgeInsetsInsetRect(CGRect,
UIEdgeInsets)&应该是好理解的。
再看如何设置&UITextField&中文本到四边的间距,这里也可以定义自己的&InsetsTextField:
//& Created by Unmi on 11/2/11.
//& Copyright (c) 2011 http://unmi.cc. All rights
@interface InsetsTextField : UITextField
@implementation InsetsTextField
placeHolder 的位置,左右缩
- (CGRect)textRectForBounds:(CGRect)bounds {
& & return CGRectInset( bounds ,
& & // CGRect inset =
CGRectMake(bounds.origin.x + 10, bounds.origin.y, bounds.size.width
- 10, bounds.size.height); //更好理解些
控制文本的位置,左右缩 20
- (CGRect)editingRectForBounds:(CGRect)bounds {
& & return CGRectInset( bounds ,
& & //CGRect inset =
CGRectMake(bounds.origin.x + 10, bounds.origin.y, bounds.size.width
- 10, bounds.size.height);
& & //& return
//-----------------------------------------------------------------
//下面是使用
InsetsTextField 的代码,可放在
viewDidLoad 等代理方法中
InsetsTextField *insetTextField = [[InsetsTextField alloc]
initWithFrame:CGRectMake(10, 10, 180, 25)];
//须手动设置它的
borderStyle, 不然看不到边框的
insetsTextField.borderStyle = UITextBorderStyleRoundedR
[self.view addSubview:insetsTextField];
[insetsTextField release];
效果如下:
上面更像是借鉴的&InsetsLabel&的实现,其实对于
UITextField&还有更好的实现办法,而且更简单,因为&UITextFiled&原来就支持的做法。比如它可以让你做出在文本框最前方固定一个
符号,表示这个文本框是输入钱的,第一个$
是不能被删除的。确实,你可以在 TextField&上贴个&Label,然后文本框的光标后移,稍显麻烦了。
UITextField&可以直接设置
leftView&或
rightView,&然后文本输入区域就在
leftView&和
rightView&之间了,看例子:
UILabel *paddingView = [[UILabel alloc] initWithFrame:CGRectMake(0,
0, 10, 25)];
paddingView.text = @"$";
paddingView.textColor = [UIColor darkGrayColor];
paddingView.backgroundColor = [UIColor clearColor];
textfield.leftView = paddingV
textfield.leftViewMode = UITextFieldViewModeAlways;rightView&也是一样的设置方式,其中的&
Mode&有四种,看到名字应该不难理解:
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
它的效果呢就更酷了:
文本框的起始光标是从上图数字
1&位置开始的。
实际应用中,对于&UITextField&如果有类似的需求,我会毫不犹豫的使用
leftView/rightView&属性来设置。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

我要回帖

更多关于 uitextfield密码 的文章

 

随机推荐