转自http://blog.sina.com.cn/s/blog_9693f61a0101dyd3.html
UITextField使用总结
UITextField* myTextField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];
myTextField.delegate = self;//委托类需要遵守UITextFieldDelegate协议
设置属性
UIControl属性对UITextField完全可以用,下面的都是UITextFiels扩展的属性:
myTextField.textAlignment = UITextAlignmentLeft;//默认就是左对齐,这个是UITextField扩展属性
myTextField.borderStyle = UITextBorderStyleBezel;//默认是没有边框,如果使用了自定义的背景图片边框会被忽略掉
myTextField.placeholder = @"请在此输入账号";//为空白文本字段绘制一个灰色字符串作为占位符
myTextField.clearsOnBeginEditing = YES;//设置为YES当用点触文本字段时,字段内容会被清除
myTextField.adjustsFontSizeToFitWidt
//myTextField.background = [UIImage imageNamed:@"registBtn"];//可以接受UIImage对象,此项设置则边框失效。
myTextField.clearButtonMode = UITextFieldViewModeUnles
//myTextField.LeftView =
//myTextField.leftViewMode =
//myTextField.RightView =
//myTextField.rightViewMode =
显示
[self.view addSubview:myTextField];
委托方法
- (BOOL)textFieldShouldBeginEdit
}
- (void)textFieldDidBeginEditing
}
- (BOOL)textFieldShouldEndEditin
}
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersIn
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
}
通知
UITextField派生自UIControl,所以UIControl类中的通知系统在文本字段中也可以使用。除了UIControl类的标准事件,你还可以使用下列UITextField类特有的事件
UITextFieldTextDidBeginE
UITextFieldTextDidChange
UITextFieldTextDidEndEdi
当文本字段退出编辑模式时触发。通知的object属性存储了最终文本。
因为文本字段要使用键盘输入文字,所以下面这些事件发生时,也会发送动作通知
UIKeyboardWillShowNotifi
键盘显示之前发送
UIKeyboardDidShowNotific
键盘显示之后发送
UIKeyboardWillHideNotifi
键盘隐藏之前发送
UIKeyboardDidHideNotific
键盘隐藏之后发送
打开键盘卷动文本字段,下面附一个通用的解决键盘遮挡的方法
//用于处理键盘遮挡的问题
- (void)moveView:(UITextField *)textField leaveView:(BOOL)leave
{
}
使用如下:
- (void)textFieldDidBeginEditing
{
}
- (void)textFieldDidEndEditing:(UITextField *)textField;
{
}
下面再补充一些重写绘制相关内容
重写绘制行为
除了UITextField对象的风格选项,你还可以定制化UITextField对象,为他添加许多不同的重写方法,来改变文本字段的显示行为。这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围。如果你创见了一个自定义的UITextField类,你可以重写这些方法,这样就可以改变一个或多个边界。一定不要直接调用 fan广发;它们都是被iPhone运行库调用的回调函数下面举个例子:
- (CGRect)clearButtonForBounds:(CGRect)bounds{
}
下列方法在创建一个UITextField的子类时可以重写:
borderRectForBounds
指定矩形边界
textRectForBounds
指定显示文本的边界
placeholderRectForBounds
指定站位文本的边界
editingRectForBounds
指定编辑中文本的边界
clearButtonRectForBounds
指定显示清除按钮的边界
leftViewRectForBounds
指定显示左附着视图的边界
rightViewRectForBounds
指定显示右附着视图的边界
下面附带一个UILabel和UITextField的重绘实现padding效果的代码
首先来看
//1.header file
#import
@interface InsetsLabel : UILabel
@property(nonatomic) UIEdgeInsets insets;
-(id) initWithFrame:(CGRect)frame andInsets: (UIEdgeInsets) insets;
-(id) initWithInsets: (UIEdgeInsets) insets;
@end
//2. implementation file
#import "InsetsLabel.h"
@implementation InsetsLabel
@synthesize insets=_insets;
-(id) initWithFrame:(CGRect)frame andInsets:(UIEdgeInsets)insets {
}
-(id) initWithInsets:(UIEdgeInsets)insets {
}
-(void) drawTextInRect:(CGRect)rect {
}
关键就是覆盖了 -(void) drawTextInRect: (CGRect) rect;
再看如何设置
//
//
//
//
#import
@interface InsetsTextField : UITextField
@end
@implementation InsetsTextField
//控制 placeHolder 的位置,左右缩 20
- (CGRect)textRectForBounds:(CGRect)bounds {
}
// 控制文本的位置,左右缩 20
- (CGRect)editingRectForBounds:(CGRect)bounds {
}
@end
//-----------------------------------------------------------------
//下面是使用 InsetsTextField 的代码,可放在 viewDidLoad 等代理方法中
InsetsTextField *insetTextField = [[InsetsTextField alloc]
//须手动设置它的 borderStyle, 不然看不到边框的
insetsTextField.borderStyle = UITextBorderStyleRounded
[self.view addSubview:insetsTextField];
[insetsTextField release];
@end
效果如下:
上面更像是借鉴的
而 UITextField
UILabel *paddingView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 25)];
paddingView.text = @"$";
paddingView.textColor = [UIColor darkGrayColor];
paddingView.backgroundColor = [UIColor clearColor];
textfield.leftView = paddingView;
textfield.leftViewMode = UITextFieldViewModeAlway
它的效果呢就更酷了:
文本框的起始光标是从上图数字 1
实际应用中,对于