UITextView和UITextField最大的區(qū)別是:
UITextView支持多行輸入应又,而UITextField只能單行輸入掐场。
實(shí)際上翻斟,UITextView繼承自UIScrollView 匿垄,UITextField繼承自UIView嚣州。
在使用上我們完全可以把UITextView看作是UITextField的加強(qiáng)版亿乳。
UITextField(輸入框)
/*
command + k 顯示或隱藏模擬器鍵盤
*/
#import "ViewController.h"
@interface ViewController ()<UITextFieldDelegate>//遵守協(xié)議
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
/*********UITextField(輸入框)**********/
//UITextField->UIControl->UIView
// UITextView和UITextField是有區(qū)別的
/*
1硝拧、創(chuàng)建
2径筏、背景顏色
3、添加
*/
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(30, 100, 260, 40)];
//textField.backgroundColor = [UIColor grayColor];
//colorWithPatternImage 將圖片對象轉(zhuǎn)成顏色對象
//平鋪的效果
//textField.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"q.jpg"]];
//邊框類型:borderStyle
/*
1障陶、UITextBorderStyleNone 無
2滋恬、UITextBorderStyleRoundedRect 圓角
3、UITextBorderStyleLine 直角 黑色
4抱究、UITextBorderStyleBezel 直角 灰色
*/
textField.borderStyle = UITextBorderStyleRoundedRect;
//文字顏色:textColor
textField.textColor = [UIColor redColor];
//字體大小:font
textField.font = [UIFont systemFontOfSize:23.0];
//對齊方式:textAlignment 水平方向?qū)R
textField.textAlignment = NSTextAlignmentLeft;
//對齊方式:垂直方向 contentVerticalAlignment
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//自適應(yīng)文字大小:adjustsFontSizeToFitWidth
textField.adjustsFontSizeToFitWidth = YES;
//最小字體的設(shè)置:minimumFontSize
textField.minimumFontSize = 23.0;
//鍵盤顏色:keyboardAppearance 翻譯:鍵盤的外觀
/*
1恢氯、UIKeyboardAppearanceAlert=UIKeyboardAppearanceDark 黑色
2、UIKeyboardAppearanceDefault=UIKeyboardAppearanceLight 白色
*/
textField.keyboardAppearance = UIKeyboardAppearanceLight;
//鍵盤樣式:keyboardType
/*
1鼓寺、UIKeyboardTypeAlphabet 符號鍵盤
2勋拟、UIKeyboardTypeDecimalPad 數(shù)字鍵盤
3、UIKeyboardTypePhonePad 數(shù)字鍵盤
*/
textField.keyboardType = UIKeyboardTypeDefault;
//return鍵樣式
/*
1妈候、UIReturnKeyDone Done
2敢靡、UIReturnKeyGoogle Search
*/
textField.returnKeyType = UIReturnKeyGoogle;
//自動大寫:autocapitalizationType
/*
1、UITextAutocapitalizationTypeAllCharacters 所有字母都大寫
2州丹、UITextAutocapitalizationTypeNone 無
3醋安、UITextAutocapitalizationTypeSentences 每個(gè)句子的首字母大寫
4、UITextAutocapitalizationTypeWords 每個(gè)單詞的首字母大寫
*/
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
//是否自動糾錯(cuò):autocorrectionType
/*
1墓毒、UITextAutocorrectionTypeDefault 默認(rèn)
2、UITextAutocorrectionTypeYes 糾錯(cuò)
3亲怠、UITextAutocorrectionTypeNo 不糾錯(cuò)
*/
textField.autocorrectionType = UITextAutocorrectionTypeNo;
//提示文字:placeholder
textField.placeholder = @"請輸入賬號";
//密文效果:secureTextEntry
textField.secureTextEntry = NO;
//背景圖片:background 邊框狀態(tài)為圓角:無效
textField.background = [UIImage imageNamed:@"1.png"];
//一鍵刪除的按鈕:出現(xiàn)狀態(tài)
/*
1所计、UITextFieldViewModeAlways 一直出現(xiàn)
2、UITextFieldViewModeNever 一直沒有 默認(rèn)效果
3团秽、UITextFieldViewModeWhileEditing 當(dāng)編輯時(shí)出現(xiàn)主胧,不編輯時(shí)消失
4、UITextFieldViewModeUnlessEditing 當(dāng)編輯時(shí)不出現(xiàn)习勤,不編輯時(shí)出現(xiàn)
*/
textField.clearButtonMode = UITextFieldViewModeUnlessEditing;
//再次編輯是否清空之前文字
textField.clearsOnBeginEditing = YES;
//默認(rèn)文字:text
textField.text = @"123456789";
//設(shè)置左視圖:leftView
//位置無效
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(1000, 800, 40, 40)];
view.backgroundColor = [UIColor orangeColor];
textField.leftView = view;
//設(shè)置左視圖出現(xiàn)狀態(tài)
textField.leftViewMode = UITextFieldViewModeAlways;
//設(shè)置右視圖:rightView
//如果存在右視圖踪栋,一鍵刪除按鈕就不存在
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
view1.backgroundColor = [UIColor redColor];
//textField.rightView = view1;
//textField.rightViewMode = UITextFieldViewModeAlways;
/*同一個(gè)視圖不要放到不同位置*/
//鍵盤上面的自定義視圖
//位置無效 寬無效
//可以在inputView上面再添加子視圖
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(120, 7880, 320, 40)];
inputView.backgroundColor = [UIColor cyanColor];
//textField.inputAccessoryView = inputView;
//自定義鍵盤:inputView
UIView *input = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
input.backgroundColor = [UIColor purpleColor];
//UIButton
//textField.inputView = input;
/********!!!!!delegate!!!!!!*******/
textField.delegate = self;
//讓輸入框開始響應(yīng):光標(biāo)出現(xiàn),鍵盤出現(xiàn)
[textField becomeFirstResponder];
[self.view addSubview:textField];
}
#pragma mark - UITextFieldDelegate
//return鍵的協(xié)議方法 *****
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//一般都在這里收回鍵盤
//讓輸入框失去第一響應(yīng):收回鍵盤图毕、光標(biāo)消失
[textField resignFirstResponder];
return YES;
}
//一鍵刪除對應(yīng)的協(xié)議方法 ***
- (BOOL)textFieldShouldClear:(UITextField *)textField{
//默認(rèn)是YES
//NO:一鍵刪除無效
if (textField.text.length == 3) {
return NO;
}
return YES;
}
//輸入框是否可以開始編輯 ***
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
//YES:可以編輯 NO:不可以編輯
return YES;
}
//輸入框是否可以結(jié)束編輯 ***
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
//YES:可以結(jié)束編輯 NO:不可以結(jié)束編輯
// if (textField.text.length < 11) {
// return NO;
// }
return YES;
}
//當(dāng)輸入框文字出現(xiàn)變化就響應(yīng)的方法 **
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//range : location length
NSLog(@"%lu %lu",(unsigned long)range.length, (unsigned long)range.location);
NSLog(@"%@",string);
return YES;
}
//輸入框開始響應(yīng)就調(diào)用的方法 ****
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"begin");
}
//輸入框結(jié)束響應(yīng)就調(diào)用的方法 *****
- (void)textFieldDidEndEditing:(UITextField *)textField{
//獲取輸入框里面的文字內(nèi)容:text
NSLog(@"%@",textField.text);
NSLog(@"end");
}
@end
UITextView
#import "ViewController.h"
@interface ViewController ()<UITextViewDelegate>
@property (nonatomic, strong) UILabel *placeholderLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//關(guān)閉自動布局
self.automaticallyAdjustsScrollViewInsets = NO;
/*******UITextView*********/
//UITextView->UIScrollView->UIView
//無:提示文字夷都、密文、邊框予颤、一鍵刪除囤官、自適應(yīng)文字大小、左右視圖
//實(shí)例化
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 100, 220, 100)];
//背景顏色
textView.backgroundColor = [UIColor grayColor];
//更改字體顏色
textView.textColor = [UIColor redColor];
//更改字體大小
textView.font = [UIFont systemFontOfSize:22.0];
//更改對齊方式
textView.textAlignment = NSTextAlignmentLeft;
//更改鍵盤顏色
textView.keyboardAppearance = UIKeyboardAppearanceDark;
//更改鍵盤類型
textView.keyboardType = UIKeyboardTypeEmailAddress;
//自動大寫
textView.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
//自動糾錯(cuò)
textView.autocorrectionType = UITextAutocorrectionTypeYes;
//return鍵樣式
textView.returnKeyType = UIReturnKeyDone;
//默認(rèn)文字
textView.text = @"";
//鍵盤上面的自定義視圖
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
view.backgroundColor = [UIColor orangeColor];
textView.inputAccessoryView = view;
//自定義鍵盤 inputView
//是否允許滑動
textView.scrollEnabled = YES;
//隱藏垂直滑塊
textView.showsVerticalScrollIndicator = NO;
//是否可以編輯
textView.editable = YES;
//突出文字 高亮
textView.dataDetectorTypes = UIDataDetectorTypeAll;
/*******!!!delegate!!!!******/
textView.delegate = self;
//添加
[self.view addSubview:textView];
//placeholder的功能
_placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 210, 40)];
_placeholderLabel.text = @"請輸入文字";
_placeholderLabel.textColor = [UIColor lightGrayColor];
_placeholderLabel.font = [UIFont systemFontOfSize:22.0];
[textView addSubview:_placeholderLabel];
if (textView.text.length != 0) {
_placeholderLabel.hidden = YES;
}else{
_placeholderLabel.hidden = NO;
}
}
#pragma mark - UITextViewDelegate
//是否可以開始輸入文字
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
return YES;
}
//是否可以結(jié)束編輯
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
return YES;
}
//網(wǎng)址是否可以連接
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
return YES;
}
//文字出現(xiàn)改變就執(zhí)行:文字能否更改
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
//點(diǎn)擊return收回鍵盤
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
}
return YES;
}
//- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange{
//
//}
//輸入框開始響應(yīng)就執(zhí)行
- (void)textViewDidBeginEditing:(UITextView *)textView{
}
//輸入框失去響應(yīng)就執(zhí)行
- (void)textViewDidEndEditing:(UITextView *)textView{
}
//文字出現(xiàn)改變就執(zhí)行
- (void)textViewDidChange:(UITextView *)textView{
if (textView.text.length != 0) {
_placeholderLabel.hidden = YES;
}else{
_placeholderLabel.hidden = NO;
}
}
//更改文字
//- (void)textViewDidChangeSelection:(UITextView *)textView{
// textView.text = @"123yu";
//}
@end