0、寫在前面api
一、小技巧app
UILabel類:字體
1-1-1)、設置行間距富文本,有省略號要求的,須要再次設置省略(初始化時設置的會失效)。動畫
UITextField類:spa
1-2-1)、清空按鈕。代理
UITextView類:server
1-3-1)、UITextView只能x軸居中,y軸須要手動調。blog
UITextField、UITextView類共有:事件
1-4-1)、在鍵盤上面顯示一個右邊有「完成」按鈕的ToolBar。rem
1-4-2)、協議 UITextInputTraits(自動大寫、糾錯、鍵盤相關)。
1-4-3)、點擊跳轉到新頁面
二、字體計算、自適應
三、鍵盤相關
0、寫在前面
一、UILabel:
一、沒有自帶選擇、複製功能
二、無佔位符
三、多行
二、UITextField:
一、
二、有佔位符
三、一行
三、UITextView:
一、
二、無佔位符
三、多行
N、富文本,再次刷新,須要先置nil
一、小技巧:
UILabel類:
1-1-1)、設置行間距富文本,有省略號要求的,須要再次設置省略(初始化時設置的會失效)。
// 最後面的,以"..."結束 self.contentLabel.lineBreakMode = NSLineBreakByTruncatingTail;
UITextField類:
1-2-1)、清空按鈕。
// 當編輯時纔出現 self.inputTF.clearButtonMode = UITextFieldViewModeWhileEditing;
1-2-N)、在tableView中響應。
1)、好比,輸入用戶名的cell,UITextField輸入框在右邊,左邊有@「用戶名」的label。想要整個cell響應輸入框。可在cell的點擊row代理中,獲取當前cell的textField,而後becomeFirstResponder,用戶體驗好點。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 獲取cell KJPersonalSettingCell *cell = [tableView cellForRowAtIndexPath:indexPath]; // 彈出輸入法 [cell.inputTF becomeFirstResponder]; }
2)、好比,生日用datePicker,選中顯示在cell,若是是用UITextField(考慮cell複用:如當前cell樣式右邊無label,有UITextField),可設置UITextField的enabled = NO,而後用在cell的點擊row代理中跳出datePicker。
// cell複用,刷新數據中設置爲NO,就不響應輸入事件 self.inputTF.enabled = NO; // 而後由cell點擊事件彈出 datePicker - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { }
UITextView類:
1-3-1)、UITextView只能x軸居中,y軸須要手動調
--修改自簡書 《IOS UITextView內容垂直居中方法》 --木頭Lee
- (void)contentSizeToFit { //先判斷一下有沒有文字(沒文字就不必設置居中了) if([msgTextView.text length]>0) { //textView的contentSize屬性 CGSize contentSize = msgTextView.contentSize; //textView的內邊距屬性 UIEdgeInsets offset; //若是文字內容高度沒有超過textView的高度 if(contentSize.height <= msgTextView.height) { //textView的高度減去文字高度除以2就是Y方向的偏移量,也就是textView的上內邊距 CGFloat offsetY = (msgTextView.height - contentSize.height)/2; offset = UIEdgeInsetsMake(offsetY, 0, 0, 0); } //根據前面計算設置textView的ContentSize和Y方向偏移量 [msgTextView setContentInset:offset]; } }
UITextField、UITextView類共有:
1-4-1)、在鍵盤上面顯示一個右邊有「完成」按鈕的ToolBar。
UIToolbar *numberToolBar= [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 40)]; numberToolBar.items = [NSArray arrayWithObjects: [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)], nil]; //numberToolBar.tintColor=[UIColor redColor]; self.inputTF.inputAccessoryView = numberToolBar; - (void)doneWithNumberPad { }
1-4-2)、協議 UITextInputTraits(自動大寫、糾錯、鍵盤相關):
①、autocapitalizationType:自動首字母大寫
②、autocorrectionType:自動糾錯
③、spellCheckingType:拼寫檢查
④、keyboardType:鍵盤類型
⑤、keyboardAppearance:鍵盤顏色樣式
⑥、returnKeyType:返回的按鍵「字」:發送、下一個...
⑦、enablesReturnKeyAutomatically:當文字輸入長度爲0,失能鍵盤的return,但文字輸入長度大於0,使能鍵盤的return
⑧、secureTextEntry:密碼輸入,輸完一段時間、或輸入下一個字體,會變成*
⑨、textContentType:把本身的通信錄內容,變成鍵盤上的一個ToolBar按鈕,快速填入。第三方鍵盤好像不支持。
1-4-3)、點擊跳轉到新頁面
①、設置代理
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ SearchVC *vc = [[SearchVC alloc]init]; [self.navigationController pushViewController:vc animated:YES]; return NO; } // 同 - (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
②、跳轉過去,跳出鍵盤
-(void)viewWillAppear:(BOOL)animated{ [self.searchTF becomeFirstResponder]; }
二、字體計算、自適應
1)、固定的Frame,自適應Font大小,如數量增減,1和1000。
[label1 setAdjustsFontSizeToFitWidth:YES];
2)、固定的Font,自適應Frame,用於信息類顯示
[label2 sizeToFit];
3)、固定的Font,獲取自適應Frame值,反過來設置Label的Frame,用於信息類顯示。這裏的100是等下設置Label的width,也是返回的rect.frame.size.width
CGRect rect = [templabel.text boundingRectWithSize:CGSizeMake(100, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:templabel.font} context:nil];
後續補充:
"3)" 有誤,100爲限制的最大寬度,可是,若是不足100,仍是會返回正確的值。不是都返回100。
"1)"、"2)" ,自適應尺寸的,要記得設置size。沒有邊界如何自適應。(由於通常在約束Lable,都沒約束size,故特地寫下)
三、鍵盤相關
0)、鍵盤強制顯示、隱藏
// 成爲第一響應(彈起) [self.tfView becomeFirstResponder]; // 取消第一響應(隱藏) [self.tfView resignFirstResponder];
1)、鍵盤事件通知
1-1)、彈出鍵盤可能蓋住TextField。監聽鍵盤的通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoradChangeAction:) name:UIKeyboardWillChangeFrameNotification object:nil];
1-2)、keyBoradChangeAction方法裏接收通知,duration是鍵盤動畫時間,keyBoardNewY是鍵盤當前的y軸位置,keyBoardOldY主要和當前鍵盤位置比較。(接着要移動評論框或者移動後面的ScrollView均可以)
- (void)keyBoradChangeAction:(NSNotification*)noti { CGFloat duration = [[noti.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]; CGFloat keyBoardOldY = [[noti.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue].origin.y; CGFloat keyBoardNewY = [[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].origin.y; //一、經過重置約束條件 //1-1-1)、xib拉過來 或 原生約束 //self.theBottomSpace.constant = ?; //1-1-2)、Masonry約束make返回的值 //theBottomWidth.mas_equalTo(); //1-1-3)、Masonry代碼更新約束 //self.theBottomview mas_updateConstraints [UIView animateWithDuration:duration animations:^{ //1-2)、更新約束( 在1-1-1 或 1-1-2 或 1-1-3 的基礎上跟新約束 ) //[self.view layoutIfNeeded]; //二、經過改變Frame if ( (int)(keyBoardOldY - keyBoardNewY) > 0 ) { [self showKeyBoardModelWithY:keyBoardNewY]; } else { [self showNomalModel]; } }]; }
2)、dealloc記得移除
[[NSNotificationCenter defaultCenter]removeObserver:self];
3)、touchesBegan:withEvent && scrollViewDidScroll -->屏幕點擊&&屏幕滑動要取消編輯狀態
//不建議,有的View有偏移量的,會有問題。 //[self.view endEditing:YES]; //誰監聽的,讓誰去放棄第一響應 [self.tfView resignFirstResponder];
後續補充:若有用IQKeyboard ,須要關閉
- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; // 若是是view,能夠加在init [[IQKeyboardManager sharedManager] setEnable:NO]; } -(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; // 若是是view,能夠加在dealloc [[IQKeyboardManager sharedManager] setEnable:YES]; }
後續補充:如今有出現一種狀況,就是cell有響應點擊事件,而又想經過點擊其餘地方取消鍵盤,結果卻進入了cell的點擊事件裏。
解決方法:一、可考慮加個透明、有手勢識別的View ?
二、跳轉到新的輸入頁面。絕大部分APP的作法,同時還能多顯示,輸入記錄等信息!