ibireme / YYText

Powerful text framework for iOS to display and edit rich text.
MIT License
8.84k stars 1.67k forks source link

系统语音识别功能无法工作 #975

Closed CodeSlaveZhang closed 1 year ago

CodeSlaveZhang commented 1 year ago

[Dictation] UIDictationController could not find the last hypothesis "一二" in range '(null)'. The likely cause is that something modified the text store or the hypothesis during dictation.

Given text range '(null)' we could not find the last hypothesis "一二". The likely cause is that something modified the text store or the hypothesis during dictation.

[Dictation] UIDictationController could not find the last hypothesis "123" in range '(null)'. The likely cause is that something modified the text store or the hypothesis during dictation.

Given text range '(null)' we could not find the last hypothesis "123". The likely cause is that something modified the text store or the hypothesis during dictation.

[Dictation] UIDictationController could not find the last hypothesis "12345" in range '(null)'. The likely cause is that something modified the text store or the hypothesis during dictation.

Given text range '(null)' we could not find the last hypothesis "12345". The likely cause is that something modified the text store or the hypothesis during dictation.

CodeSlaveZhang commented 1 year ago

已解决 第一步:替换这个函数。 原因是 _selectedTextRange = [self _correctedTextRange:_selectedTextRange]; 这行代码放在了replaceCharactersInRange之前,导致selectedTextRange不对 - (void)_replaceRange:(YYTextRange *)range withText:(NSString *)text notifyToDelegate:(BOOL)notify{ if (NSEqualRanges(range.asRange, _selectedTextRange.asRange)) { if (notify) [_inputDelegate selectionWillChange:self]; NSRange newRange = NSMakeRange(0, 0); newRange.location = _selectedTextRange.start.offset + text.length; _selectedTextRange = [YYTextRange rangeWithRange:newRange]; if (notify) [_inputDelegate selectionDidChange:self]; } else { if (range.asRange.length != text.length) { if (notify) [_inputDelegate selectionWillChange:self]; NSRange unionRange = NSIntersectionRange(_selectedTextRange.asRange, range.asRange); if (unionRange.length == 0) { // no intersection if (range.end.offset <= _selectedTextRange.start.offset) { NSInteger ofs = (NSInteger)text.length - (NSInteger)range.asRange.length; NSRange newRange = _selectedTextRange.asRange; newRange.location += ofs; _selectedTextRange = [YYTextRange rangeWithRange:newRange]; } } else if (unionRange.length == _selectedTextRange.asRange.length) { // target range contains selected range _selectedTextRange = [YYTextRange rangeWithRange:NSMakeRange(range.start.offset + text.length, 0)]; } else if (range.start.offset >= _selectedTextRange.start.offset && range.end.offset <= _selectedTextRange.end.offset) { // target range inside selected range NSInteger ofs = (NSInteger)text.length - (NSInteger)range.asRange.length; NSRange newRange = _selectedTextRange.asRange; newRange.length += ofs; _selectedTextRange = [YYTextRange rangeWithRange:newRange]; } else { // interleaving if (range.start.offset < _selectedTextRange.start.offset) { NSRange newRange = _selectedTextRange.asRange; newRange.location = range.start.offset + text.length; newRange.length -= unionRange.length; _selectedTextRange = [YYTextRange rangeWithRange:newRange]; } else { NSRange newRange = _selectedTextRange.asRange; newRange.length -= unionRange.length; _selectedTextRange = [YYTextRange rangeWithRange:newRange]; } } if (notify) [_inputDelegate selectionDidChange:self]; } } if (notify) [_inputDelegate textWillChange:self]; NSRange newRange = NSMakeRange(range.asRange.location, text.length); [_innerText replaceCharactersInRange:range.asRange withString:text]; [_innerText yy_removeDiscontinuousAttributesInRange:newRange]; _selectedTextRange = [self _correctedTextRange:_selectedTextRange]; if (notify) [_inputDelegate textDidChange:self]; }

第二步: 添加以下几个函数以及实现 `-(void)insertDictationResult:(NSArray<UIDictationPhrase > )dictationResult{ // _preparingDictation = NO; NSMutableString phraseResult = @"".mutableCopy; for (UIDictationPhrase phrase in dictationResult) { [phraseResult appendString:phrase.text]; } [self insertText:phraseResult]; }

-(id)insertDictationResultPlaceholder{ return nil;//如果不实现则insertDictationResult的插入文本会被覆盖 }

-(void)removeDictationResultPlaceholder:(id)placeholder willInsertResult:(BOOL)willInsertResult{ //do nothing 相应的由于实现了insertDictationResultPlaceholder,则这里识别完毕后删除也不需要做任何事情 } `

CodeSlaveZhang commented 1 year ago

https://github.com/CodeSlaveZhang/YYText/blob/master/YYText/YYTextView.m

winterSleep commented 1 year ago

https://github.com/CodeSlaveZhang/YYText/blob/master/YYText/YYTextView.m

1465 行的代码,在你提交的源文件中并没有被删除,所以仍然会有问题,把1465 行删除就可以了。 @CodeSlaveZhang