武汉ios培训
达内武汉中心

15271940953

热门课程

武汉ios培训:UITextField控件

  • 时间:2016-06-02
  • 发布:Developer_Man
  • 来源:C博客

武汉ios培训师讲到UITextField用来接受用户输入的文本,在开发中十分常见,今天我们来学习一下该控件。


代码实例
ZYViewController.h:
#import <UIKit/UIKit.h>  
 
@interface ZYViewController : UIViewController  
 
@property (nonatomic,strong)UITextField *myTextField;  
 
@end  
ZYViewController.m:

@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:

#import <UIKit/UIKit.h>  
 
@interface ZYUITextFieldViewController : UIViewController<UITextFieldDelegate>  
 
@property(nonatomic,strong) UITextField *myTextField;  
 
@property(nonatomic,strong) UILabel *labelCounter;  
 
@end  
ZYUITextFieldViewController.m:

@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 resignFirstResponder];//关闭键盘  
    return YES;  

上一篇:武汉ios培训:UILabel
下一篇:UITabBarController多视图控制器

想学ios,到哪学比较好

学习ios开发的最佳之选是什么?

学ios,有前途吗?

成为ios开发工程师,培训和自学哪条路更适合你?

选择城市和中心
贵州省

广西省

海南省