UItextField一般用於外部數據輸入,以實現人機交互。好比咱們QQ、微信的登陸界面中讓你輸入帳號和密碼的地方api
#import "ViewController.h"微信
@interface ViewController ()ide
{字體
UITextField *_textField;spa
}3d
@end代理
@implementation ViewControllerorm
- (void)viewDidLoad {server
[super viewDidLoad];blog
self.view.backgroundColor = [UIColor blackColor];
// 建立TextField
[self creatTextField];
// TextField屬性設置
[self setTextFieldPro];
// TextField文本屬性設置
[self setTextOfTextFieldPro];
// TextField keyBoard設置
[self setKeyBoardOfTextField];
}
- (void)creatTextField
{
_textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 300, 50)];
_textField.backgroundColor = [UIColor yellowColor];
[self.view addSubview:_textField];
}
- (void)setTextFieldPro
{
// 1. 設置邊框
// UITextBorderStyleNone, 沒有邊框
// UITextBorderStyleLine, 線性邊框
// UITextBorderStyleBezel, 尖角邊框
// UITextBorderStyleRoundedRect // 圓角矩形
_textField.borderStyle = UITextBorderStyleLine;
// 2. 設置圖片(設置邊框不能設置UITextBorderStyleRoundedRect,不然沒有效果)
// _textField.background = [UIImage imageNamed:@"11"];
// 3. 設置編輯狀態(NO,用戶點擊沒有響應)
_textField.enabled = YES;
}
- (void)setTextOfTextFieldPro
{
// 1.默認文字
// _textField.text = @"奔跑吧,少年";
// 2. 設置字體顏色
_textField.textColor = [UIColor redColor];
// 3. 設置文字的對齊方式
// _textField.textAlignment = NSTextAlignmentCenter;
// 4. 設置文字的大小
_textField.font = [UIFont systemFontOfSize:30];
// 5. 設置佔位文字
// _textField.placeholder = @"請輸入密碼";
// 6. 清除原有文字
_textField.clearsOnBeginEditing = YES;
// 讓_textField成爲鍵盤的第一響應者
[_textField becomeFirstResponder];
// 判斷是否是在編輯狀態
BOOL boo = _textField.isEditing;
// 設置清除按鈕何時顯示
_textField.clearButtonMode = UITextFieldViewModeAlways;
// 設置_textField 左視圖(左右視圖只能顯示一個)
UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
leftImageView.image = [UIImage imageNamed:@"TM"];
_textField.leftView = leftImageView;
_textField.leftViewMode = UITextFieldViewModeAlways;
// _textField.rightView = leftImageView;
// _textField.rightViewMode = UITextFieldViewModeAlways;
// 密文顯示
// _textField.secureTextEntry = YES;
// 是否自動大小寫
// UITextAutocapitalizationTypeNone,
// UITextAutocapitalizationTypeWords, // 單詞首字母大寫
// UITextAutocapitalizationTypeSentences,// 句子首字母大寫
// UITextAutocapitalizationTypeAllCharacters // 全大寫
// _textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
}
- (void)setKeyBoardOfTextField
{
// 設置鍵盤的顏色
_textField.keyboardAppearance = UIKeyboardAppearanceDark;
// 設置鍵盤的類型
// UIKeyboardTypeNumberPad 只能輸入數字
_textField.keyboardType = UIKeyboardTypeURL;
// 返回按鈕的樣式
_textField.returnKeyType = UIReturnKeyNext;
// 設置鍵盤的二級鍵盤
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
imageView.image = [UIImage imageNamed:@"TM"];
_textField.inputAccessoryView = imageView;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 移除第一響應者 (鍵盤退出)
[_textField resignFirstResponder];
NSLog(@"%@",_textField.text);
}
@end
@interface ViewController ()<UITextFieldDelegate>
{
UITextField *_textField;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addTextFieldToView];
}
- (void)addTextFieldToView
{
_textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 80)];
_textField.backgroundColor = [UIColor lightGrayColor];
_textField.font = [UIFont systemFontOfSize:40];
_textField.placeholder = @"請輸入文字";
_textField.clearButtonMode = UITextFieldViewModeAlways;
// 設置代理
_textField.delegate = self;
[self.view addSubview:_textField];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(@"將要編輯");
// YES 能夠繼續編輯 NO 不讓編輯
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
NSLog(@"---%@",textField.text);
NSLog(@"將要結束編輯的時候");
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"已經開始編輯");
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(@"已經結束編輯");
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[_textField resignFirstResponder];
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
NSLog(@"清除的時候");
// NO 不讓清除 YES 讓清除
return NO;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"點擊了Return按鈕的時候");
return YES;
}
#pragma mark - 用戶每次輸入信息的或刪除的時候都調用
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSLog(@"%ld-- %@",range.length,string);
return YES;
}
@end
#import "ViewController.h"
@interface ViewController ()
{
UITextField *_textField;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 添加UITextField
[self addTextField];
// 添加檢測給鍵盤
[self keyBaordShowOrHide];
}
- (void)addTextField
{
CGFloat textFieldX = 20;
CGFloat textFieldW = self.view.frame.size.width - 2 * textFieldX;
CGFloat textFieldH = 50;
CGFloat textFieldY = 500;
_textField = [[UITextField alloc] initWithFrame:CGRectMake(textFieldX, textFieldY, textFieldW, textFieldH)];
_textField.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:_textField];
}
- (void)keyBaordShowOrHide
{
// 檢測鍵盤彈出
// 1. 誰去檢測
// 2. 檢測到鍵盤彈出執行什麼方法
// 3. 區別消息是否是鍵盤彈出的消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyBoard:) name:UIKeyboardWillShowNotification object:nil];
//檢測鍵盤消失
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyBoard:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)hideKeyBoard:(NSNotification *)sender
{
NSLog(@"鍵盤消失");// 輸入框還原
// view 位置還原
self.view.transform = CGAffineTransformIdentity;
}
- (void)showKeyBoard:(NSNotification *)sender
{
NSLog(@"鍵盤彈起");
NSLog(@"%@",sender);
// 獲取鍵盤的高度
CGRect rect = [[sender.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyBoardH = rect.size.height;
// 讓整個屏幕往上移動一個鍵盤的高度
self.view.transform = CGAffineTransformMakeTranslation(0, - keyBoardH + 100);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[_textField resignFirstResponder];
}
@end