IOS开发(13)之UITextField控件
1 前言
UITextField用来接受用户输入的文本,在开发中十分常见,今天我们来学习一下该控件。
2 代码实例
ZYViewController.h:
[plain]
#import <UIKit/UIKit.h>
@inte易做图ce ZYViewController : UIViewController
@property (nonatomic,strong)UITextField *myTextField;
@end
#import <UIKit/UIKit.h>
@inte易做图ce ZYViewController : UIViewController
@property (nonatomic,strong)UITextField *myTextField;
@end
ZYViewController.m:
[plain]
@synthesize myTextField;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
CGRect textFieldFrame = CGRectMake(0.0f, 0.0f, 200.0f, 31.0f);
self.myTextField = [[UITextField alloc] initWithFrame:textFieldFrame];//绘制控件模型
self.myTextField.borderStyle = UITextBorderStyleRoundedRect;//设置如何显示边框
self.myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;//设置纵向居中
self.myTextField.textAlignment = UITextAlignmentCenter;//设置水平居中
self.myTextField.text = @"Sir Archy Develop_Zhang";//设置输入框内容
self.myTextField.center = self.view.center;//设置UITextField位置
[self.view addSubview:self.myTextField];
}
@synthesize myTextField;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
CGRect textFieldFrame = CGRectMake(0.0f, 0.0f, 200.0f, 31.0f);
self.myTextField = [[UITextField alloc] initWithFrame:textFieldFrame];//绘制控件模型
self.myTextField.borderStyle = UITextBorderStyleRoundedRect;//设置如何显示边框
self.myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;//设置纵向居中
self.myTextField.textAlignment = UITextAlignmentCenter;//设置水平居中
self.myTextField.text = @"Sir Archy Develop_Zhang";//设置输入框内容
self.myTextField.center = self.view.center;//设置UITextField位置
[self.view addSubview:self.myTextField];
}
ZYUITextFieldViewController.h:
[plain]
#import <UIKit/UIKit.h>
@inte易做图ce ZYUITextFieldViewController : UIViewController<UITextFieldDelegate>
@property(nonatomic,strong) UITextField *myTextField;
@property(nonatomic,strong) UILabel *labelCounter;
@end
#import <UIKit/UIKit.h>
@inte易做图ce ZYUITextFieldViewController : UIViewController<UITextFieldDelegate>
@property(nonatomic,strong) UITextField *myTextField;
@property(nonatomic,strong) UILabel *labelCounter;
@end
ZYUITextFieldViewController.m:
[plain]
@synthesize myTextField;
@synthesize labelCounter;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
CGRect textFieldFrame = CGRectMake(38.0f, 30.0f, 220.0f, 31.0f);
self.myTextField = [[UITextField alloc] initWithFrame:textFieldFrame];
self.myTextField.delegate = self;
self.myTextField.borderStyle = UITextBorderStyleRoundedRect;
self.myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
// self.myTextField.text = @"Sir Archy Developer_Zhang";
self.myTextField.placeholder = @"Enter text here......";//当UITextField里面没有字符的时候,默认显示的内容
[self.view addSubview:myTextField];
CGRect labelCounterFrame = self.myTextField.frame;
labelCounterFrame.origin.y += textFieldFrame.size.height+10;
self.labelCounter = [[UILabel alloc] initWithFrame:labelCounterFrame];
[self.view addSubview:labelCounter];
[self calculateAndDisplayTextFieldLengthWithText:self.myTextField.text];
UILabel *currencyLabel = [[UILabel alloc] initWithFrame:CGRectZero];//CGRectZero相当于CGRectMake(0,0,0,0)
currencyLabel.text = [[[NSNumberFormatter alloc] init] currencySymbol];//初始化Label内容返回接受者的本地货币符号
currencyLabel.font = self.myTextField.font;
[currencyLabel sizeToFit];//调整和移动接收者的视图,它只是包含它自己的视图。
self.myTextField.leftView = currencyLabel;
self.myTextField.leftViewMode = UITextFieldViewModeAlways;//设置左视图一直显示
/*
typedef enum{
UITextFieldViewModeNever,//从不显示
UITextFieldViewModeWhileEditing,//编辑时候显示
UITextFieldViewModeUnlessEditing,//编辑结束时候显示
UITextFieldViewModeAlways//一直显示
}UITextFieldViewMode;
*/
}
//计算UITextField里面字符的长度
-(void)calculateAndDisplayTextFieldLengthWithText:(NSString *)paramText{
NSString *characterOrCharacters = @"Characters";
if ([paramText length]==1) {
characterOrCharacters = @"Character";
}
self.labelCounter.text = [NSString stringWithFormat:@"%lu %@",(unsigned long)[paramText length],characterOrCharacters];
}
//当UITextField输入文字后触发的事件
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
BOOL result = YES;
if ([textField isEqual:self.myTextField]) {
NSString *wholeText = [textField.text stringByReplacingCharactersInRange:range withString:string];//追加后输入的字符串
[self calculateAndDisplayTextFieldLengthWithText:wholeText];//重新计算字符长度
}
return result;
}
//当单击返回按钮触发的事件
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFir
补充:移动开发 , IOS ,