0、寫在前面 1、小技巧 UILabel類: 1-1-1)、設置行間距富文本,有省略號要求的,需要再次設置省略(初始化時設置的會失效)。 UITextField類: 1-2-1)、清空按鈕。 UITextView類: 1-3-1)、UITextView只能x軸居中,y軸需要手動調。 UITextFi ...
0、寫在前面
1、小技巧
UILabel類:
1-1-1)、設置行間距富文本,有省略號要求的,需要再次設置省略(初始化時設置的會失效)。
UITextField類:
1-2-1)、清空按鈕。
UITextView類:
1-3-1)、UITextView只能x軸居中,y軸需要手動調。
UITextField、UITextView類共有:
1-4-1)、在鍵盤上面顯示一個右邊有“完成”按鈕的ToolBar。
1-4-2)、協議 UITextInputTraits(自動大寫、糾錯、鍵盤相關)。
1-4-3)、點擊跳轉到新頁面
2、字體計算、自適應
3、鍵盤相關
0、寫在前面
1、UILabel:
1、沒有自帶選擇、複製功能
2、無占位符
3、多行
2、UITextField:
1、
2、有占位符
3、一行
3、UITextView:
1、
2、無占位符
3、多行
1、小技巧:
UILabel類:
1-1-1)、設置行間距富文本,有省略號要求的,需要再次設置省略(初始化時設置的會失效)。
// 最後面的,以"..."結束 self.contentLabel.lineBreakMode = NSLineBreakByTruncatingTail;
UITextField類:
1-2-1)、清空按鈕。
// 當編輯時才出現 self.inputTF.clearButtonMode = UITextFieldViewModeWhileEditing;
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]; }
2、字體計算、自適應
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,故特意寫下)
3、鍵盤相關
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-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]; //2、通過改變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的點擊事件里。
解決方法:1、可考慮加個透明、有手勢識別的View ?
2、跳轉到新的輸入頁面。絕大部分APP的做法,同時還能多顯示,輸入記錄等信息!