Believe in your own future, will thank yourself right now Sinner Yun

Sinner_Yun

TextField

 

 

UITextField文本输入框

 

 

【方法】

 

//背景文字

@property(nonatomic,copy) NSString *placeholder;

 

//正文

@property(nonatomic,copy) NSString *text;

 

//设置文本框的边框风格,可以设置为圆角矩形边框风格,默认风格为None

@property(nonatomic) UITextBorderStyle borderStyle;

 

//背景图(如果是圆角风格,图片将会失效,否则会显示图片,风格失效)

@property(nonatomic,retain) UIImage *background; 

 

//文字超过范围以后自动变小

@property(nonatomic) BOOL adjustsFontSizeToFitWidth;

//最小能到几号

@property(nonatomic) CGFloat minimumFontSize; 

 

//水平对齐方式(参考label)(一般用默认的左)

@property(nonatomic) NSTextAlignment textAlignment;

 

//垂直对齐方式(一般用默认的中间对齐)

@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment;

 

//键盘类型

@property(nonatomic) UIKeyboardType keyboardType;

//回车键类型

@property(nonatomic) UIReturnKeyType returnKeyType;

 

//安全输入模式(暗文加密)

@property(nonatomic,getter=isSecureTextEntry) BOOL secureTextEntry;

 

//激活输入状态时清空之前的内容

@property(nonatomic) BOOL clearsOnBeginEditing;

 

//清空按钮的出现模式

@property(nonatomic) UITextFieldViewMode  clearButtonMode;

 

//左view及出现模式

@property(nonatomic,retain) UIView *leftView; 

@property(nonatomic) UITextFieldViewMode leftViewMode; 

 

//右view及出现模式

@property(nonatomic,retain) UIView *rightView;   

@property(nonatomic) UITextFieldViewMode rightViewMode;

 

//这个属性默认就是键盘,如果设置某个view那么弹出来的就不是键盘,而是自己设置的view

@property (readwrite, retain) UIView *inputView;

//这个view是随着键盘一起弹出的view

@property (readwrite, retain) UIView *inputAccessoryView;

 

- (BOOL)becomeFirstResponder;//响应输入状态

- (BOOL)resignFirstResponder;//结束输入状态

 

 

UITextFieldDelegate 【代理方法】

//开始编辑

- (void)textFieldDidBeginEditing:(UITextField *)textField;

//结束编辑

- (void)textFieldDidEndEditing:(UITextField *)textField;

//回车时触发

- (BOOL)textFieldShouldReturn:(UITextField *)textField;

 

 

viewcontroller方法

 

 

//弹出一个新的viewController

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion;

 

//销毁当前viewcontroller

- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion;

 

 

 

 

 

 

 

 

 

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor = [UIColor yellowColor];

    

    //创建一个文本输入框(一般用作输入账号密码等)

    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(40, 60, 240, 50)];

    

    //提示语句

    tf.placeholder = @"请输入账号";

    

    //边框风格

    tf.borderStyle = UITextBorderStyleRoundedRect;

    

    tf.backgroundColor = [UIColor redColor];

    

    //设置背景图,如果边框风格不是圆角,那么风格失效。

    //如果边框风格是圆角,那么背景图失效

    tf.background = [UIImage imageNamed:@"aa"];

    

    tf.font = [UIFont systemFontOfSize:30];

    

    //文字范围超过输入框时,文字会自动变小

    tf.adjustsFontSizeToFitWidth = YES;

    

    //文字自动变小的下限

    tf.minimumFontSize = 20;

    

    //左右对齐方式(同label)

    tf.textAlignment = NSTextAlignmentLeft;

    

    //上下对齐方式(btn的对齐方式也是这样设置)

    tf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

    

    //在开始编辑的时候清空内容

    tf.clearsOnBeginEditing = NO;

    

    //清空按钮的出现模式

    tf.clearButtonMode = UITextFieldViewModeAlways;

    

    //安全输入(一般用于输入密码)

    tf.secureTextEntry = NO;

    

    //设置键盘的类型

    tf.keyboardType = UIKeyboardTypeDefault;

    

    //设置回车键的外观(只是外观,和功能本身没有关系)

    tf.returnKeyType = UIReturnKeyDone;

    

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

    view.backgroundColor = [UIColor greenColor];

    

    //设置左view和出现模式

    tf.leftView = view;

    tf.leftViewMode = UITextFieldViewModeAlways;

    

    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];

    blueView.backgroundColor = [UIColor blueColor];

    

    //设置键盘

//    tf.inputView = blueView;

    //键盘的附属view

    tf.inputAccessoryView = blueView;

    

    [self.view addSubview:tf];

    tf.tag = 1;

}

 

//点击self.view时会触发此方法

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    UITextField *tf = (UITextField *)[self.view viewWithTag:1];

    

    //收起键盘

    [tf resignFirstResponder];

}

 

 

 

 

 

 

 

 

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor = [UIColor yellowColor];

    

    UITextField *nameTF = [[UITextField alloc] initWithFrame:CGRectMake(40, 60, 240, 40)];

    nameTF.borderStyle = UITextBorderStyleRoundedRect;

    nameTF.placeholder = @"请输入QQ号";

    nameTF.tag = 1;

    nameTF.delegate = self;

    [self.view addSubview:nameTF];

}

 

//文本输入框被激活输入状态时响应

- (void)textFieldDidBeginEditing:(UITextField *)textField

{

    NSLog(@"tag = %d",textField.tag);

}

 

//结束输入状态时响应

- (void)textFieldDidEndEditing:(UITextField *)textField

{

    NSLog(@"end = %d",textField.tag);

}

 

//点击回车键的时候触发

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    NSLog(@"text = %@",textField.text);

    

    return YES;

}

 

//每次输入的时候都会触发

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

    NSLog(@"%d===%@",range.location,string);

    

    if (range.location == 10) {

        return NO;

    }

    

    return YES;

}

 

 

 

 

 

 

 

 

 

 

          

 

//  LoginViewController.m

//  A3login

//

//  Created by MS on 15-3-19.

//  Copyright (c) 2015年 qf. All rights reserved.

//

 

#import "LoginViewController.h"

#import "HomeViewController.h"

#import "RegisterViewController.h"

 

@interface LoginViewController () <RegisterDelegate>

 

@end

 

@implementation LoginViewController

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [self.view endEditing:YES];

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor redColor];

    

    UITextField *nameTF = [[UITextField alloc] initWithFrame:CGRectMake(40, 60, 240, 40)];

    nameTF.borderStyle = UITextBorderStyleRoundedRect;

    nameTF.placeholder = @"请输入用户名";

    [self.view addSubview:nameTF];

    

    UITextField *passTF = [[UITextField alloc] initWithFrame:CGRectMake(40, 140, 240, 40)];

    passTF.borderStyle = UITextBorderStyleRoundedRect;

    passTF.placeholder = @"请输入密码";

    passTF.secureTextEntry = YES;

    [self.view addSubview:passTF];

    

    nameTF.tag = 11;

    passTF.tag = 12;

    

    UIButton *logBtn = [UIButton buttonWithType:UIButtonTypeSystem];

    logBtn.frame = CGRectMake(60, 220, 60, 30);

    [logBtn setTitle:@"login" forState:UIControlStateNormal];

    logBtn.backgroundColor = [UIColor grayColor];

    [logBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    logBtn.tag = 1;

    [self.view addSubview:logBtn];

    

    UIButton *registBtn = [UIButton buttonWithType:UIButtonTypeSystem];

    registBtn.frame = CGRectMake(200, 220, 60, 30);

    [registBtn setTitle:@"register" forState:UIControlStateNormal];

    registBtn.backgroundColor = [UIColor grayColor];

    [registBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    registBtn.tag = 2;

    [self.view addSubview:registBtn];

}

 

- (void)btnClick:(UIButton *)sender

{

    NSLog(@"tag = %ld",sender.tag);

    

    if (sender.tag == 2) {

        

        RegisterViewController *rvc = [[RegisterViewController alloc] init];

        

        rvc.delegate = self;

        

        [self presentViewController:rvc animated:YES completion:nil];

        

        return;

    }

    

    

    UITextField *nameTF = (UITextField *)[self.view viewWithTag:11];

    

    //如果文本输入框里的文字长度为0

    if (!nameTF.text.length) {

        NSLog(@"请输入用户名");

        return;

    }

    

    HomeViewController *hvc = [[HomeViewController alloc] init];

    

    hvc.nameStr = nameTF.text;

    

    //弹出一个新的视图

    [self presentViewController:hvc animated:YES completion:nil];

}

 

//委托的回调方法(代理方法、协议方法)

- (void)sendInfoName:(NSString *)nameStr password:(NSString *)passStr

{

    NSLog(@"%@====%@",nameStr,passStr);

    UITextField *nameTF = (UITextField *)[self.view viewWithTag:11];

    UITextField *passTF = (UITextField *)[self.view viewWithTag:12];

    

    nameTF.text = nameStr;

    passTF.text = passStr;

}

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

 

 

 

 

//  HomeViewController.h

//  A3login

//

//  Created by MS on 15-3-19.

//  Copyright (c) 2015年 qf. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@interface HomeViewController : UIViewController

 

@property (nonatomic, copy) NSString *nameStr;

 

@end

 

 

 

 

 

 

 

 

 

//  HomeViewController.m

//  A3login

//

//  Created by MS on 15-3-19.

//  Copyright (c) 2015年 qf. All rights reserved.

//

 

#import "HomeViewController.h"

 

@interface HomeViewController ()

 

@end

 

@implementation HomeViewController

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    //退出当前页面(必须是被present出来的才能dismiss)

    [self dismissViewControllerAnimated:YES completion:nil];

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor yellowColor];

    

    UILabel *label = [[UILabel alloc] initWithFrame:self.view.bounds];

    label.text = self.nameStr;

    [self.view addSubview:label];

}

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

 

 

 

 

//  RegisterViewController.h

//  A3login

//

//  Created by MS on 15-3-19.

//  Copyright (c) 2015年 qf. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@protocol RegisterDelegate <NSObject>

 

- (void)sendInfoName:(NSString *)nameStr password:(NSString *)passStr;

 

@end

 

 

 

@interface RegisterViewController : UIViewController

 

@property (nonatomic, assign) id <RegisterDelegate> delegate;

 

@end

 

 

 

 

 

 

//  RegisterViewController.m

//  A3login

//

//  Created by MS on 15-3-19.

//  Copyright (c) 2015年 qf. All rights reserved.

//

 

#import "RegisterViewController.h"

 

@interface RegisterViewController () <UITextFieldDelegate>

 

@end

 

@implementation RegisterViewController

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [self.view endEditing:YES];

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor purpleColor];

    

    NSArray *titleArr = [NSArray arrayWithObjects:@"用户名", @"密码", @"手机号", @"邮箱", nil];

    for (int i = 0; i<4; i++) {

        UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(40, 50+i*60, 240, 30)];

        tf.borderStyle = UITextBorderStyleRoundedRect;

        tf.placeholder = titleArr[i];

        if (i == 3) {

            tf.returnKeyType = UIReturnKeyDone;

        } else {

            tf.returnKeyType = UIReturnKeyNext;

        }

        tf.delegate = self;

        tf.tag = 100+i;

        [self.view addSubview:tf];

    }

    

    UIButton *logBtn = [UIButton buttonWithType:UIButtonTypeSystem];

    logBtn.frame = CGRectMake(60, 300, 60, 30);

    [logBtn setTitle:@"确定" forState:UIControlStateNormal];

    logBtn.backgroundColor = [UIColor grayColor];

    [logBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    logBtn.tag = 1;

    [self.view addSubview:logBtn];

    

    UIButton *registBtn = [UIButton buttonWithType:UIButtonTypeSystem];

    registBtn.frame = CGRectMake(200, 300, 60, 30);

    [registBtn setTitle:@"取消" forState:UIControlStateNormal];

    registBtn.backgroundColor = [UIColor grayColor];

    [registBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    registBtn.tag = 2;

    [self.view addSubview:registBtn];

}

 

- (void)btnClick:(UIButton *)sender

{

    if (sender.tag == 1) {

        UITextField *nameTF = (UITextField *)[self.view viewWithTag:100];

        UITextField *passTF = (UITextField *)[self.view viewWithTag:101];

        

        [self.delegate sendInfoName:nameTF.text password:passTF.text];

    }

    

    [self dismissViewControllerAnimated:YES completion:nil];

}

 

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    if (textField.tag == 103) {

        //取消输入状态

        [textField resignFirstResponder];

    } else {

        //找到下一个文本框

        UITextField *tf = (UITextField *)[self.view viewWithTag:textField.tag + 1];

        

        //让文本输入框激活输入状态

        [tf becomeFirstResponder];

    }

    

    return YES;

}

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

posted on 2014-03-19 21:03  Sinner_Yun  阅读(296)  评论(0编辑  收藏  举报

导航