|
@@ -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];
|