我正在尝试为科学应用创建自定义自动填充字段(例如在搜索栏中),该定义允许将定义添加到UITextView
。该过程将是这样。
UITextView
中添加“阻止” TableView?
,并带有元素周期表元素的建议。我对此很陌生,所以在此先感谢您对所有这些命名约定的任何建议以及有关如何实现此目标的任何相关信息或解释。
答案 0 :(得分:0)
这是实现它的简单方法。
我已经使用名为DropDown的第三方库来显示将特定单词添加到UITextView
中时的下拉列表。
import UIKit
import DropDown
class ViewController: UIViewController, UITextViewDelegate {
@IBOutlet weak var textView: UITextView!
let dropDown = DropDown()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func textViewDidChange(_ textView: UITextView) {
if textView.text.last == "#" {
let endPosition: UITextPosition = textView.endOfDocument
//Get courser position to show dropdown
let rect = textView.caretRect(for: endPosition)
//Create a custom view to provide anchorView to DropDown
let customView = UIView(frame: CGRect(x: rect.origin.x, y: rect.origin.y, width: 200, height: 0))
textView.addSubview(customView)
dropDown.anchorView = customView // UIView or UIBarButtonItem
// The list of items to display. Can be changed dynamically
dropDown.dataSource = ["Car", "Motorcycle", "Truck"]
// Action triggered on selection
dropDown.selectionAction = { [unowned self] (index: Int, item: String) in
print("Selected item: \(item) at index: \(index)")
//remove Custom view as we no longer needed it.
customView.removeFromSuperview()
//Set your text accourding to selection from dropdown item.
textView.text = textView.text.dropLast() + " " + "\(item)"
}
dropDown.show()
}
}
}
我添加了评论以供解释。
您的结果将是:
HERE是有关更多信息的演示项目。