限制UITextView的字數和字數監控,表情異常的狀況和禁用表情

限制UITextView的字數和字數監控,表情異常的狀況和禁用表情


3523FD80CC4350DE0AE7F89A8532B9A8.png

由於字數佔一個字符,表情佔兩個字符。你要是限制15個字符的話,全打表情的話,最後一個表情會出現錯誤,因此要使用監聽,對UITextViewTextDidChangeNotification監聽,想用的能夠直接拿來用,廢話很少說,直接上代碼!!UITextfield也是同樣,只不過把監聽寫爲UITextfieldTextDidChangeNotification

//移除監聽 -(void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UITextViewTextDidChangeNotification" object:TtextView]; } -(void)show{ //輸入詳細 TtextView = [[UITextView alloc]initWithFrame:CGRectMake(5*khScale, 5*khScale, self.view.frame.size.width-10*kScale, 160*khScale)]; TtextView.backgroundColor = [UIColor whiteColor]; TtextView.font = [UIFont systemFontOfSize:15*kScale]; TtextView.tag = 301; TtextView.delegate = self; [self.view addSubview:TtextView]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged9:) name:@"UITextViewTextDidChangeNotification" object:TtextView];
NSInteger textlength9; - (void)textFiledEditChanged9:(UITextView *)textView { NSString *toBeString = TtextView.text; //獲取高亮部分 UITextRange *selectedRange = [TtextView markedTextRange]; UITextPosition *position = [TtextView positionFromPosition:selectedRange.start offset:0]; // 沒有高亮選擇的字,則對已輸入的文字進行字數統計和限制 if (!position) { if (toBeString.length > 200 && TtextView.markedTextRange == nil) { //用字符串的字符編碼指定索引查找位置 NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:200]; if (rangeIndex.length == 1) { TtextView.text = [toBeString substringToIndex:200]; } else { //用字符串的字符編碼指定區域段查找位置 // NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, Max_Length)]; TtextView.text = [toBeString substringWithRange:NSMakeRange(0, textlength9)]; } } else { textlength9 = toBeString.length; } } } - (void)textViewDidChangeSelection:(UITextView *)textView{ _textNumberLabel.text = [NSString stringWithFormat:@"%lu/200",(unsigned long)TtextView.text.length]; if (TtextView.text.length >=200) { _textNumberLabel.textColor = [UIColor redColor]; } else{ _textNumberLabel.textColor = [UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1.0]; } [self textChanged]; } -(void)textChanged{ CGRect orgRect = TtextView.frame;//獲取原始UITextView的frame CGSize size = [TtextView sizeThatFits:CGSizeMake(TtextView.frame.size.width, MAXFLOAT)]; orgRect.size.height=size.height+10;//獲取自適應文本內容高度 if (orgRect.size.height > 200) { noteTextHeight = orgRect.size.height; } [self updateViewsFrame]; } - (void)updateViewsFrame{ if (!allViewHeight) { allViewHeight = 0; } if (!noteTextHeight) { noteTextHeight = 100; } } - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ _textNumberLabel.text = [NSString stringWithFormat:@"%lu/200 ",(unsigned long)TtextView.text.length]; if (textView == TtextView) { //這裏的if時候爲了獲取刪除操做,若是沒有次if會形成當達到字數限制後刪除鍵也不能使用的後果. if (range.length == 1 && text.length == 0) { return YES; } //so easy else if (TtextView.text.length > 200) { TtextView.text = [TtextView.text substringToIndex:200]; return NO; } } return YES; } -(void)textViewDidChange:(UITextView *)textView { label.text = textView.text; if (textView.text.length == 0) { label.text = @"點擊輸入描述(200字之內)"; }else{ label.text = @""; } }
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ // 限制蘋果系統輸入法 禁止輸入表情 if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) { return NO; } return YES; }
相關文章
相關標籤/搜索