【发布时间】:2019-05-23 05:39:49
【问题描述】:
我想检查用户是否在 UITextView 中为科学应用键入了特定短语。我有这个当前代码,它创建一个最后 20 个字符的字符串,然后检查该字符串是否包含 arrayOfWords 中的一个单词,该单词在 textViewDidChange 上执行:
var arrayOfWords = [“Gold", “Phosphorus”]
func textViewDidChange(_ textField: UITextView) {
if let selectedRange = textView.selectedTextRange {
let cursorPosition = textView.offset(from: textView.beginningOfDocument, to: selectedRange.start)
let startRange = cursorPosition - 20
let result = textView.text.substring(from: startRange, to: cursorPosition)
checkIfContains(textViewString: result)
}}
func checkIfContains(textViewString: String) {
if arrayOfWords.contains(where: textViewString.contains) {
doAction()
}
}
我知道这是一个非常具体的功能,因此对于我如何实现此功能的任何想法将不胜感激。
【问题讨论】:
-
用空格分隔字符串并检查最后分隔的部分是否可以转换为浮点数,或者让用户按下按钮来检查字符串是否是特定的应用程序
-
@Lu_ “用空格分隔字符串”是什么意思。如何做到这一点?
标签: ios swift uitextview uitextviewdelegate