IOS开发(13)之UITextField控件

1 前言

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

2 代码实例

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;
}
运行结果:


3 结语

以上就是UITextField的简单介绍,希望对大家有所帮助。

Demo实例下载:http://download.csdn.net/detail/u010013695/5294545

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值