-2
當用戶在UITextView
中輸入特定搜索關鍵字時輸入「@」時,是否有任何顯示下拉列表的方法?當用戶輸入「@」時從列表中搜索數據UITextView中的符號
例如:
類型「@ios」和所有包含在下拉列表列表中顯示單詞「IOS」過濾字符串。
當用戶在UITextView
中輸入特定搜索關鍵字時輸入「@」時,是否有任何顯示下拉列表的方法?當用戶輸入「@」時從列表中搜索數據UITextView中的符號
例如:
類型「@ios」和所有包含在下拉列表列表中顯示單詞「IOS」過濾字符串。
我通過不斷地檢查用戶鍵入的文本來實現這一點。
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
// if user type any latter from @,space and new line then clear the search string
if text == "@" || text == " " || text == "\n" {
self.searchString = ""
}
// append the type string in text view text
let resultString = textView.text.stringByAppendingString(text)
// break the line with @ symbol and find the occurence
let numberOfOccurrences = resultString.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "@")).count - 1
// store all words for checking
let stringArray = resultString.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "@")) ?? [String]()
// checking if string array > 1 that means string contains @ symbol
self.searchString = stringArray.count > 1 ? stringArray[numberOfOccurrences].containsString(" ") ? "" : stringArray[numberOfOccurrences] : ""
if self.searchString.characters.count > 0 {
self.getTopSearchFromLive(self.searchString)
} else {
self.tableView.hidden = true
}
return true
}
我認爲你正在尋找的東西像this.This代表將被稱爲每當用戶按下按鍵@
試試這個: -
func textView(_ textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if (textView.text.appending(text) == "@") {
//trigger '@' operation
if arrayObject.contains(value) {
//Populate array and display tableview.
}
}
else if (textView.text.appending(text) == "/") {
//trigger/operation
}
//Same conditions go on
}
查看以下鏈接並根據您的要求進行修改。 https://github.com/EddyBorja/MLPAutoCompleteTextField https://www.raywenderlich.com/336/auto-complete-tutorial-for-ios-how-to-auto-complete-with-custom-values – Venkat