浏览代码

输入框输入限制字符优化

Steven 8 月之前
父节点
当前提交
2a420413fd

+ 40 - 6
KulexiuForStudent/KulexiuForStudent/Module/Chat/Group/View/ChatComplainBodyView.m

@@ -61,16 +61,50 @@
     [self endEditing:YES];
 }
 
--(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
-    // 输入控制
-    NSString *newString = [textView.text stringByReplacingCharactersInRange:range withString:text];
-    if (newString.length > 200) {
-        return NO;
+- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
+    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;
     }
-    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;
 }

+ 62 - 9
KulexiuForStudent/KulexiuForStudent/Module/Chat/GroupNotice/View/NoticeEditBodyView.m

@@ -86,22 +86,75 @@
 }
 
 - (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;
-        }
+        limitCount = 20;
     }
     else {
-        NSString *newText = [[textView text] stringByReplacingCharactersInRange:range withString:text];
-        if (newText.length > 255) {
-            return NO;
-        }
+        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;
 }
 
+- (void)updateTextViewLineHeight:(UITextView *)textView {
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
+        return;
+    }
+    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
+    paragraphStyle.lineSpacing = 4.0f;
+    UIFont *font = [UIFont systemFontOfSize:15 weight:UIFontWeightRegular];
+    [UIView setAnimationsEnabled:NO];
+    NSRange selectedRange = textView.selectedRange;
+    NSMutableAttributedString *attrs = [[NSMutableAttributedString alloc] initWithString:textView.text attributes:@{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:font, NSForegroundColorAttributeName:HexRGB(0x333333)}];
+    textView.attributedText = attrs;
+    textView.selectedRange = selectedRange;
+    [UIView setAnimationsEnabled:YES];
+}
+
+- (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];
+}
+
 
 - (BOOL)textViewShouldEndEditing:(UITextView *)textView {
     [self endEditing:YES];

+ 34 - 3
KulexiuForStudent/KulexiuForStudent/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];

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

@@ -126,11 +126,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) {
@@ -139,6 +139,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];
+}
+
 - (void)setButtonColor:(UIColor *)buttonColor {
     _buttonColor = buttonColor;
     if (buttonColor) {

+ 37 - 5
KulexiuForStudent/KulexiuForStudent/Module/Mine/Homework/View/AccompanyAlertView.m

@@ -82,16 +82,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];
+}
+
 /*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.

+ 41 - 7
KulexiuForStudent/KulexiuForStudent/Module/Mine/Works/View/MusicPublicContentView.m

@@ -70,17 +70,51 @@
     return YES;
 }
 
--(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
-    
-    // 输入控制
-    NSString *newString = [textView.text stringByReplacingCharactersInRange:range withString:text];
-    if (newString.length > 150) {
-        return NO;
+- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
+    UITextRange *markedTextRange = textView.markedTextRange;
+    if (markedTextRange) {
+        // 当前处于拼音输入状态,暂不更新 attributedText
+        return YES;
+    }
+
+    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;
 }