// Copyright ? 2016年 你國(guó)哥. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
{
UITextField *field;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/*
UITextField 文本輸入框
*/
//1.創(chuàng)建
field = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 200, 50)];
//2.顯示
[self.view addSubview:field];
//3.屬性
/*
繼承自UIView的屬性: tag backgroundColor hidden userInteractionEnabled
邊框樣式:
UITextBorderStyleNone, 默認(rèn)->無邊框
UITextBorderStyleLine, 邊線
UITextBorderStyleBezel, 立體陰影
UITextBorderStyleRoundedRect 圓角矩形
*/
field.borderStyle = UITextBorderStyleRoundedRect;
//設(shè)置輸入文本的字號(hào)
field.font = [UIFont systemFontOfSize:26];
//設(shè)置文本的顏色
field.textColor = [UIColor greenColor];
//設(shè)置對(duì)齊的方式
field.textAlignment = NSTextAlignmentCenter;
//設(shè)置文本內(nèi)容
field.text = @"設(shè)置文本內(nèi)容";
屏幕快照 2016-02-26 上午10.24.51.png
/*
設(shè)置輸入文本的大寫方式
UITextAutocapitalizationTypeNone,系統(tǒng)默認(rèn)
UITextAutocapitalizationTypeWords,單詞首字母大寫
UITextAutocapitalizationTypeSentences,句子首字母大寫
UITextAutocapitalizationTypeAllCharacters,所有字母大寫
*/
// field.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
//輸入框?yàn)榭帐堑奶崾疚谋? field.placeholder = @"請(qǐng)輸入賬號(hào)";
屏幕快照 2016-02-26 上午10.29.11.png
//開啟安全輸入
field.secureTextEntry = YES;
屏幕快照 2016-02-26 上午10.30.51.png
/*
輸入框的清除按鈕:輸入框有內(nèi)容的情況
UITextFieldViewModeNever, 永遠(yuǎn)不顯示x
UITextFieldViewModeWhileEditing, 編輯時(shí)顯示x
UITextFieldViewModeUnlessEditing, 非編輯時(shí)顯示x
UITextFieldViewModeAlways 永遠(yuǎn)顯示x
*/
field.clearButtonMode = UITextFieldViewModeUnlessEditing;
//第一響應(yīng)者
/*
KeyBoard 鍵盤
*/
/*
UIReturnType: return鍵的樣式
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
UIReturnKeyContinue
*/
field.returnKeyType = UIReturnKeySend;
/*
鍵盤類型
UIKeyboardTypeDefault,系統(tǒng)默認(rèn)
UIKeyboardTypeASCIICapable,
UIKeyboardTypeNumbersAndPunctuation,
UIKeyboardTypeURL,
UIKeyboardTypeNumberPad, 輸入數(shù)字時(shí)的鍵盤
UIKeyboardTypePhonePad, 打電話時(shí)的鍵盤
UIKeyboardTypeNamePhonePad,
UIKeyboardTypeEmailAddress,
UIKeyboardTypeDecimalPad
UIKeyboardTypeTwitter
UIKeyboardTypeWebSearch
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
*/
field.keyboardType = UIKeyboardAppearanceDefault;
//代理
field.delegate = self;
}
- (IBAction)tap:(UIButton *)sender {
//成為第一相應(yīng)者
// [field becomeFirstResponder];
//失去第一響應(yīng)者
[field resignFirstResponder];
}
/*———————————————————————————代理設(shè)計(jì)模式————————————————————————————————————*/
//A中定義協(xié)議c 添加一個(gè)屬性di<c>
//B成為A的delegate B實(shí)現(xiàn)<c>的方法
#pragma mark --UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSLog(@"將要開始編輯時(shí)");
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"已經(jīng)開始編輯時(shí)");
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
NSLog(@"將要結(jié)束編輯時(shí)");
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"已經(jīng)結(jié)束編輯時(shí)");
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSLog(@"修改文本內(nèi)容");
NSLog(@"%@",NSStringFromRange(range));
NSLog(@"%@",string);
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
NSLog(@"清空");
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@"發(fā)送了內(nèi)容:%@",field.text);
return YES;
}
@end
border //邊
roundedrect //圓角矩形
sentences //句子
mark //記號(hào)
delegate //代表
range //范圍
clear //清空
placeholder //占位符