瀏覽代碼

输入框输入限制字符优化

Steven 9 月之前
父節點
當前提交
7256f1a875

+ 38 - 9
KulexiuForTeacher/KulexiuForTeacher/Module/Chat/Group/View/ChatComplainBodyView.m

@@ -53,21 +53,50 @@
     [self endEditing:YES];
 }
 
--(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
-    if ([text isEqualToString:@"\n"]) {
-        [self endEditing:YES];
+- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
         return YES;
     }
-    
-    // 输入控制
-    NSString *newString = [textView.text stringByReplacingCharactersInRange:range withString:text];
-    if (newString.length > 200) {
-        return NO;
+
+    NSString *newText = [[textView text] stringByReplacingCharactersInRange:range withString:text];
+    if (newText.length > 200) {
+        newText = [newText substringWithRange:NSMakeRange(0, 200)];
+        textView.text = newText;
     }
-    self.countLabel.text = [NSString stringWithFormat:@"%zd/200", newString.length];
     return YES;
 }
 
+- (void)updateTextViewLineHeight:(UITextView *)textView {
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
+        return;
+    }
+    [UIView setAnimationsEnabled:YES];
+}
+
+- (void)textViewDidChange:(UITextView *)textView {
+    // 获取当前高亮的部分(如果正在拼音输入状态)
+        UITextRange *selectedRange = [textView markedTextRange];
+        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
+        
+        // 如果没有高亮选择的文本,说明不是拼音输入状态
+    if (!position) {
+        // 获取当前textView的内容
+        NSString *currentText = textView.text;
+        
+        // 如果文本超出最大长度,进行截取
+        if (currentText.length > 200) {
+            NSString *limitedText = [currentText substringToIndex:200];
+            textView.text = limitedText;
+        }
+        self.countLabel.text = [NSString stringWithFormat:@"%zd/200",textView.text.length];
+    }
+    [self updateTextViewLineHeight:textView];
+}
+
 + (CGFloat)getViewHeight {
     return 480.0f;
 }

+ 41 - 11
KulexiuForTeacher/KulexiuForTeacher/Module/Chat/GroupNotice/View/NoticeEditBodyView.m

@@ -109,20 +109,22 @@
 }
 
 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
+        return YES;
+    }
+    NSInteger limitCount = 0;
     if (textView == self.titleView) {
-        NSString *newText = [[textView text] stringByReplacingCharactersInRange:range withString:text];
-        if (newText.length > 25) {
-            return NO;
-        }
-        self.titleCount.text = [NSString stringWithFormat:@"%zd/25",newText.length];
+        limitCount = 20;
     }
     else {
-        NSString *newText = [[textView text] stringByReplacingCharactersInRange:range withString:text];
-        if (newText.length > 200) {
-            return NO;
-        }
-        self.contentCount.text = [NSString stringWithFormat:@"%zd/200",newText.length];
-
+        limitCount = 200;
+    }
+    NSString *newText = [[textView text] stringByReplacingCharactersInRange:range withString:text];
+    if (newText.length > limitCount) {
+        newText = [newText substringWithRange:NSMakeRange(0, limitCount)];
+        textView.text = newText;
     }
     return YES;
 }
@@ -145,6 +147,34 @@
 }
 
 - (void)textViewDidChange:(UITextView *)textView {
+    // 获取当前高亮的部分(如果正在拼音输入状态)
+        UITextRange *selectedRange = [textView markedTextRange];
+        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
+        
+        // 如果没有高亮选择的文本,说明不是拼音输入状态
+    if (!position) {
+        // 获取当前textView的内容
+        NSInteger limitCount = 0;
+        if (textView == self.titleView) {
+            limitCount = 25;
+        }
+        else {
+            limitCount = 200;
+        }
+        NSString *currentText = textView.text;
+        
+        // 如果文本超出最大长度,进行截取
+        if (currentText.length > limitCount) {
+            NSString *limitedText = [currentText substringToIndex:limitCount];
+            textView.text = limitedText;
+        }
+        if (textView == self.titleView) {
+            self.titleCount.text = [NSString stringWithFormat:@"%zd/25",textView.text.length];
+        }
+        else {
+            self.contentCount.text = [NSString stringWithFormat:@"%zd/200",textView.text.length];
+        }
+    }
     [self updateTextViewLineHeight:textView];
 }
 

+ 67 - 6
KulexiuForTeacher/KulexiuForTeacher/Module/Chat/View/TenantGroupCreate/TenantCreateGroupBodyView.m

@@ -128,14 +128,48 @@
 }
 
 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
-    // 输入控制
-    NSString *newString = [textView.text stringByReplacingCharactersInRange:range withString:text];
-    if (newString.length > 500) {
-        return NO;
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
+        return YES;
+    }
+
+    NSString *newText = [[textView text] stringByReplacingCharactersInRange:range withString:text];
+    if (newText.length > 500) {
+        newText = [newText substringWithRange:NSMakeRange(0, 500)];
+        textView.text = newText;
     }
     return YES;
 }
 
+- (void)updateTextViewLineHeight:(UITextView *)textView {
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
+        return;
+    }
+    [UIView setAnimationsEnabled:YES];
+}
+
+
+- (void)textViewDidChange:(UITextView *)textView {
+    // 获取当前高亮的部分(如果正在拼音输入状态)
+        UITextRange *selectedRange = [textView markedTextRange];
+        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
+        
+        // 如果没有高亮选择的文本,说明不是拼音输入状态
+    if (!position) {
+        // 获取当前textView的内容
+        NSString *currentText = textView.text;
+        
+        // 如果文本超出最大长度,进行截取
+        if (currentText.length > 500) {
+            NSString *limitedText = [currentText substringToIndex:500];
+            textView.text = limitedText;
+        }
+    }
+    [self updateTextViewLineHeight:textView];
+}
 
 - (BOOL)textViewShouldEndEditing:(UITextView *)textView {
     [self endEditing:YES];
@@ -147,8 +181,9 @@
 }
 
 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
-    if ([string isEqualToString:@"\n"]) {
-        [self endEditing:YES];
+    UITextRange *markedTextRange = textField.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
         return YES;
     }
     // 输入控制
@@ -159,6 +194,32 @@
     return YES;
 }
 
+- (void)textFieldDidChangeSelection:(UITextField *)textField {
+    // 获取当前高亮的部分(如果正在拼音输入状态)
+    UITextRange *selectedRange = [textField markedTextRange];
+    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
+    if (!position) {
+        // 获取当前textView的内容
+        NSString *currentText = textField.text;
+        
+        // 如果文本超出最大长度,进行截取
+        if (currentText.length > 50) {
+            NSString *limitedText = [currentText substringToIndex:50];
+            textField.text = limitedText;
+        }
+    }
+    [self updateTextFieldLineHeight:textField];
+}
+
+- (void)updateTextFieldLineHeight:(UITextField *)textField {
+    UITextRange *markedTextRange = textField.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
+        return;
+    }
+    [UIView setAnimationsEnabled:YES];
+}
+
 - (UIScrollView *)scrollView {
     if (!_scrollView) {
         _scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];

+ 36 - 8
KulexiuForTeacher/KulexiuForTeacher/Module/Course/AccompanyCourse/View/AccompanyAlertView.m

@@ -81,20 +81,48 @@
 }
 
 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
-    if ([text isEqualToString:@"\n"]) {
-        [self endEditing:YES];
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
         return YES;
     }
-    
-    // 输入控制
-    NSString *newString = [textView.text stringByReplacingCharactersInRange:range withString:text];
-    if (newString.length > 200) {
-        return NO;
-    }
 
+    NSString *newText = [[textView text] stringByReplacingCharactersInRange:range withString:text];
+    if (newText.length > 200) {
+        newText = [newText substringWithRange:NSMakeRange(0, 200)];
+        textView.text = newText;
+    }
     return YES;
 }
 
+- (void)updateTextViewLineHeight:(UITextView *)textView {
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
+        return;
+    }
+    [UIView setAnimationsEnabled:YES];
+}
+
+- (void)textViewDidChange:(UITextView *)textView {
+    // 获取当前高亮的部分(如果正在拼音输入状态)
+        UITextRange *selectedRange = [textView markedTextRange];
+        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
+        
+        // 如果没有高亮选择的文本,说明不是拼音输入状态
+    if (!position) {
+        // 获取当前textView的内容
+        NSString *currentText = textView.text;
+        
+        // 如果文本超出最大长度,进行截取
+        if (currentText.length > 200) {
+            NSString *limitedText = [currentText substringToIndex:200];
+            textView.text = limitedText;
+        }
+    }
+    [self updateTextViewLineHeight:textView];
+}
+
 /*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.

+ 37 - 9
KulexiuForTeacher/KulexiuForTeacher/Module/Live/View/CreateLiveBodyView.m

@@ -97,21 +97,49 @@
 }
 
 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
-    if ([text isEqualToString:@"\n"]) {
-        [self endEditing:YES];
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
         return YES;
     }
-    // 长度判断
-    // 输入控制
-    NSString *newString = [textView.text stringByReplacingCharactersInRange:range withString:text];
-    if (newString.length > 200) {
-        return NO;
+
+    NSString *newText = [[textView text] stringByReplacingCharactersInRange:range withString:text];
+    if (newText.length > 200) {
+        newText = [newText substringWithRange:NSMakeRange(0, 200)];
+        textView.text = newText;
     }
-    self.countLabel.text = [NSString stringWithFormat:@"%zd/200",newString.length];
-    
     return YES;
 }
 
+- (void)updateTextViewLineHeight:(UITextView *)textView {
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
+        return;
+    }
+    [UIView setAnimationsEnabled:YES];
+}
+
+- (void)textViewDidChange:(UITextView *)textView {
+    // 获取当前高亮的部分(如果正在拼音输入状态)
+        UITextRange *selectedRange = [textView markedTextRange];
+        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
+        
+        // 如果没有高亮选择的文本,说明不是拼音输入状态
+    if (!position) {
+        // 获取当前textView的内容
+        NSString *currentText = textView.text;
+        
+        // 如果文本超出最大长度,进行截取
+        if (currentText.length > 200) {
+            NSString *limitedText = [currentText substringToIndex:200];
+            textView.text = limitedText;
+        }
+        self.countLabel.text = [NSString stringWithFormat:@"%zd/200",textView.text.length];
+    }
+    [self updateTextViewLineHeight:textView];
+}
+
 
 - (BOOL)textViewShouldEndEditing:(UITextView *)textView {
     [self endEditing:YES];

+ 34 - 3
KulexiuForTeacher/KulexiuForTeacher/Module/Mine/AddressList/View/AddressDetailBodyView.m

@@ -72,17 +72,48 @@
 }
 
 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
-    if ([text isEqualToString:@"\n"]) {
-        [self endEditing:YES];
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
         return YES;
     }
+
     NSString *newText = [[textView text] stringByReplacingCharactersInRange:range withString:text];
     if (newText.length > 100) {
-        return NO;
+        newText = [newText substringWithRange:NSMakeRange(0, 100)];
+        textView.text = newText;
     }
     return YES;
 }
 
+- (void)updateTextViewLineHeight:(UITextView *)textView {
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
+        return;
+    }
+    [UIView setAnimationsEnabled:YES];
+}
+
+- (void)textViewDidChange:(UITextView *)textView {
+    // 获取当前高亮的部分(如果正在拼音输入状态)
+        UITextRange *selectedRange = [textView markedTextRange];
+        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
+        
+        // 如果没有高亮选择的文本,说明不是拼音输入状态
+    if (!position) {
+        // 获取当前textView的内容
+        NSString *currentText = textView.text;
+        
+        // 如果文本超出最大长度,进行截取
+        if (currentText.length > 100) {
+            NSString *limitedText = [currentText substringToIndex:100];
+            textView.text = limitedText;
+        }
+    }
+    [self updateTextViewLineHeight:textView];
+}
+
 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
     if ([string isEqualToString:@"\n"]) {
         [self endEditing:YES];

+ 28 - 11
KulexiuForTeacher/KulexiuForTeacher/Module/Mine/CreateStyle/View/MyStyleIntroduceCell.m

@@ -64,22 +64,31 @@
 }
 
 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
-    if ([text isEqualToString:@"\n"]) {
-        [self endEditing:YES];
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
         return YES;
     }
-    // 长度判断
-    // 输入控制
-    NSString *newString = [textView.text stringByReplacingCharactersInRange:range withString:text];
-    
-    if (newString.length > 200) {
-        return NO;
+
+    NSString *newText = [[textView text] stringByReplacingCharactersInRange:range withString:text];
+    if (newText.length > 200) {
+        newText = [newText substringWithRange:NSMakeRange(0, 200)];
+        textView.text = newText;
     }
-    self.countLabel.text = [NSString stringWithFormat:@"%zd/200",newString.length];
-    
     return YES;
 }
 
+- (void)updateTextViewLineHeight:(UITextView *)textView {
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
+        return;
+    }
+    [UIView setAnimationsEnabled:YES];
+}
+
+
+
 
 - (BOOL)textViewShouldEndEditing:(UITextView *)textView {
     [self endEditing:YES];
@@ -128,8 +137,16 @@
                 [tableView endUpdates];
             }];
         }
+        NSString *currentText = textView.text;
+        
+        // 如果文本超出最大长度,进行截取
+        if (currentText.length > 100) {
+            NSString *limitedText = [currentText substringToIndex:100];
+            textView.text = limitedText;
+        }
+        self.countLabel.text = [NSString stringWithFormat:@"%zd/200",textView.text.length];
     }
-    
+    [self updateTextViewLineHeight:textView];
 }
 - (UITableView *)tableView {
     UIView *tableView = self.superview;

+ 29 - 3
KulexiuForTeacher/KulexiuForTeacher/Module/Mine/Feedback/View/FeedbackBodyView.m

@@ -118,11 +118,11 @@
 }
 
 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
-    if ([string isEqualToString:@"\n"]) {
-        [self endEditing:YES];
+    UITextRange *markedTextRange = textField.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
         return YES;
     }
-    
     // 输入控制
     NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
     if (newString.length > 64) {
@@ -131,6 +131,32 @@
     return YES;
 }
 
+- (void)textFieldDidChangeSelection:(UITextField *)textField {
+    // 获取当前高亮的部分(如果正在拼音输入状态)
+    UITextRange *selectedRange = [textField markedTextRange];
+    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
+    if (!position) {
+        // 获取当前textView的内容
+        NSString *currentText = textField.text;
+        
+        // 如果文本超出最大长度,进行截取
+        if (currentText.length > 64) {
+            NSString *limitedText = [currentText substringToIndex:64];
+            textField.text = limitedText;
+        }
+    }
+    [self updateTextFieldLineHeight:textField];
+}
+
+- (void)updateTextFieldLineHeight:(UITextField *)textField {
+    UITextRange *markedTextRange = textField.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
+        return;
+    }
+    [UIView setAnimationsEnabled:YES];
+}
+
 /*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.

+ 38 - 4
KulexiuForTeacher/KulexiuForTeacher/Module/Mine/Setting/View/UnbindView/TenantUnbindBodyView.m

@@ -74,14 +74,48 @@
 }
 
 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
-    // 输入控制
-    NSString *newString = [textView.text stringByReplacingCharactersInRange:range withString:text];
-    if (newString.length > 200) {
-        return NO;
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
+        return YES;
+    }
+
+    NSString *newText = [[textView text] stringByReplacingCharactersInRange:range withString:text];
+    if (newText.length > 200) {
+        newText = [newText substringWithRange:NSMakeRange(0, 200)];
+        textView.text = newText;
     }
     return YES;
 }
 
+- (void)updateTextViewLineHeight:(UITextView *)textView {
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
+        return;
+    }
+    [UIView setAnimationsEnabled:YES];
+}
+
+- (void)textViewDidChange:(UITextView *)textView {
+    // 获取当前高亮的部分(如果正在拼音输入状态)
+        UITextRange *selectedRange = [textView markedTextRange];
+        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
+        
+        // 如果没有高亮选择的文本,说明不是拼音输入状态
+    if (!position) {
+        // 获取当前textView的内容
+        NSString *currentText = textView.text;
+        
+        // 如果文本超出最大长度,进行截取
+        if (currentText.length > 200) {
+            NSString *limitedText = [currentText substringToIndex:200];
+            textView.text = limitedText;
+        }
+    }
+    [self updateTextViewLineHeight:textView];
+}
+
 
 - (BOOL)textViewShouldEndEditing:(UITextView *)textView {
     [self endEditing:YES];

+ 39 - 9
KulexiuForTeacher/KulexiuForTeacher/Module/Mine/Works/View/MusicPublicContentView.m

@@ -70,21 +70,51 @@
     return YES;
 }
 
--(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
-    if ([text isEqualToString:@"\n"]) {
-        [self endEditing:YES];
+- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
         return YES;
     }
-    
-    // 输入控制
-    NSString *newString = [textView.text stringByReplacingCharactersInRange:range withString:text];
-    if (newString.length > 150) {
-        return NO;
+
+    NSString *newText = [[textView text] stringByReplacingCharactersInRange:range withString:text];
+    if (newText.length > 150) {
+        newText = [newText substringWithRange:NSMakeRange(0, 150)];
+        textView.text = newText;
     }
-    self.countLabel.text = [NSString stringWithFormat:@"%zd/150",newString.length];
     return YES;
 }
 
+- (void)updateTextViewLineHeight:(UITextView *)textView {
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
+        return;
+    }
+    [UIView setAnimationsEnabled:YES];
+}
+
+- (void)textViewDidChange:(UITextView *)textView {
+    // 获取当前高亮的部分(如果正在拼音输入状态)
+        UITextRange *selectedRange = [textView markedTextRange];
+        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
+        
+        // 如果没有高亮选择的文本,说明不是拼音输入状态
+    if (!position) {
+        // 获取当前textView的内容
+        NSString *currentText = textView.text;
+        
+        // 如果文本超出最大长度,进行截取
+        if (currentText.length > 150) {
+            NSString *limitedText = [currentText substringToIndex:150];
+            textView.text = limitedText;
+        }
+        self.countLabel.text = [NSString stringWithFormat:@"%zd/150",textView.text.length];
+    }
+    [self updateTextViewLineHeight:textView];
+}
+
+
 + (CGFloat)getViewHeight {
     return 230.0f;
 }

+ 37 - 10
KulexiuForTeacher/KulexiuForTeacher/Module/TXClassRoom/View/CloseCourse/KSCloseCourseView.m

@@ -81,22 +81,49 @@
     }
 }
 
--(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
-    if ([text isEqualToString:@"\n"]) {
-        [self endEditing:YES];
+- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
         return YES;
     }
-    // 输入控制
-    NSString *newString = [textView.text stringByReplacingCharactersInRange:range withString:text];
-    if (newString.length > 250) {
-        return NO;
+
+    NSString *newText = [[textView text] stringByReplacingCharactersInRange:range withString:text];
+    if (newText.length > 250) {
+        newText = [newText substringWithRange:NSMakeRange(0, 250)];
+        textView.text = newText;
     }
-//    if ([NSString isContainsTwoEmoji:text]) {
-//        return NO;
-//    }
     return YES;
 }
 
+- (void)updateTextViewLineHeight:(UITextView *)textView {
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
+        return;
+    }
+    [UIView setAnimationsEnabled:YES];
+}
+
+- (void)textViewDidChange:(UITextView *)textView {
+    // 获取当前高亮的部分(如果正在拼音输入状态)
+        UITextRange *selectedRange = [textView markedTextRange];
+        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
+        
+        // 如果没有高亮选择的文本,说明不是拼音输入状态
+    if (!position) {
+        // 获取当前textView的内容
+        NSString *currentText = textView.text;
+        
+        // 如果文本超出最大长度,进行截取
+        if (currentText.length > 250) {
+            NSString *limitedText = [currentText substringToIndex:250];
+            textView.text = limitedText;
+        }
+    }
+    [self updateTextViewLineHeight:textView];
+}
+
 
 - (BOOL)textViewShouldEndEditing:(UITextView *)textView {
     [self endEditing:YES];