上一篇简述了UITextField的属性及方法,本篇实际应用练习使用。
具体属性及方法可以参考上一篇UITextField简介:UITextField简介
首先看一下效果图:
实现了3位4位4位的手机账号格式,限制11位账号,验证是不是手机号码,UITextField属性和方法的运用等等。具体参考如下代码:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
#import "ViewController.h"
@interface ViewController ()<UITextFieldDelegate>
{
NSString *previousTextFieldContent;
UITextRange *previousSelection;
}
@property (nonatomic, strong) UITextField *login;
@property (nonatomic, strong) UITextField *password;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//创建控件
[self creatControl];
}
- (void)creatControl
{
//lable
NSArray *array = @[@"手机账号: ", @"登录密码: "];
for (int i = 0; i < 2; i++) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 100 + 50 * i, 80, 30)];
label.text = array[i];
[self.view addSubview:label];
}
//账号textField初始化
self.login = [[UITextField alloc] initWithFrame:CGRectMake(110, 100, 200, 30)];
//提醒文字
_login.placeholder = @"请输入手机号";
//键盘外观
_login.keyboardAppearance= UIKeyboardAppearanceDefault;
//键盘右下角按钮样式
_login.returnKeyType = UIReturnKeyNext;
//键盘右下角完成点击事件
[_login addTarget:self action:@selector(next)