我正在为iOS开发一个键盘扩展,在replaceDocumentText函数中处理输入之前的上下文时遇到了一个问题。该函数应该处理输入光标前后的文本,但我注意到,处理输入前上下文的循环(documentContextBeforeInput)只有在我关闭并重新打开键盘后才执行。
我正在使用KeyboardKit扩展。
`
func replaceDocumentText(textProxy: UITextDocumentProxy, suggestionsRequired: Bool=false)
async -> (context: String, count: Int) {
var startingOffset = 0
var context = ""
var forwardContextCount = 0
//show loading only if suggestions required
if suggestionsRequired {
self.loadingState = .loading
}
while let previousContext = textProxy.documentContextBeforeInput, !previousContext.isEmpty {
/// Check for the context before the cursor
context = previousContext + context
startingOffset += previousContext.count
textProxy.adjustTextPosition(byCharacterOffset: -previousContext.count)
try? await Task.sleep(nanoseconds: 50_000_000)
}
// Adjust cursor from where it started
textProxy.adjustTextPosition(byCharacterOffset: context.count)
try? await Task.sleep(nanoseconds: 1000_000_000)
while let nextContext = textProxy.documentContextAfterInput, !nextContext.isEmpty {
/// Check for the context After the cursor
forwardContextCount += 1
context += nextContext
textProxy.adjustTextPosition(byCharacterOffset: nextContext.count)
try? await Task.sleep(nanoseconds: 100_000_000)
}
self.promptText = context
self.promptCount = context.count
return await withCheckedContinuation({ continuation in
if suggestionsRequired {
Task{
await self.getSuggestions()
}
}
continuation.resume(returning: (self.promptText, self.promptCount))
})
}``
字符串
当我在textField中有文本时,我按下按钮来读取上下文,但这一行永远不会执行,我相信这是因为“.isEmpty”条件while let previousContext = textProxy.documentContextBeforeInput, !previousContext.isEmpty
但是我有文本在textField只要我关闭我的键盘扩展并重新打开它,现在按下按钮,一切都会顺利工作。请帮我这个忙。
我尝试了一些文本的情况下,但无法找出问题
1条答案
按热度按时间bttbmeg01#
字符串