问题
项目需要满足以下需求:
- 用户在输入问题题目时,自动为用户在末尾补一个“?”;
- 用户使用中文输入法时,输入内容未确认时,不触发补全逻辑;
以下是详细 UE:
中文输入法下,输入内容未确认时,不触发补全逻辑 中文输入法下,输入内容确认后,在问题的末尾自动补一个“?” 中文输入法下,多行输入内容未确认时,textView 高度应正常更新,不触发自动补全逻辑解决方案
通过 -[UITextInput markedTextRange]
方法,判断用户是否确认了当前输入内容:
- (void)textViewDidChange:(UITextView *)textView
{
/* Title */
if (textView.tag == kWBNoteProQAPublicQuestionComposeViewControllerTextViewTagTitle)
{
/* If text can be selected, it can be marked. Marked text represents provisionally
* inserted text that has yet to be confirmed by the user. It requires unique visual
* treatment in its display. If there is any marked text, the selection, whether a
* caret or an extended range, always resides witihin.
*
* Setting marked text either replaces the existing marked text or, if none is present,
* inserts it from the current selection. */
/* markedTextRange returns nil, inserted text has been confirmed by the user */
if (![textView markedTextRange])
{
// Since iOS8, 9 Keyboard Association Input will skip the `-[ textView:shouldChangeTextInRange:replacementText:]` delegate method
// so the Competion code should be here.
if (self.question.title.length == 0
&& textView.text.length > 0)
{
NSString *text = textView.text;
NSRange selectedRange = textView.selectedRange;
textView.attributedText = [WBNoteProAttributedStringGenerator titleWithOriginText:[NSString stringWithFormat:@"%@?", text]];
textView.selectedRange = selectedRange;
}
self.question.title = textView.text // code for saving textView.text data
}
...// code for updating textView height
}
}
很多文章通过
- [UITextInput positionFromPosition:offset:
是否有值来判断用户是否确认当前输入内容,个人觉得-[UITextInput markedTextRange]
已经足够进行判断了。
网友评论