Customizing QuickType Suggestions in iOS

≯℡__Kan透↙ 提交于 2019-12-08 03:25:51

问题


Well, I suspect that this is a forlorn question, ultimately doomed to sadden and disappoint me, but I want to have the question on the record for the future.

I will add this to the existing questions: here and here

The scenario is that I am creating an administration app that is designed to allow folks to edit the values in a database on a server. I have a fairly zippy API that can be used to allow REST-style data exchange.

I have the ability to download a list of values that the user can apply as search terms when they log in, and I'd like to be able to help them to enter things quickly.

For example, town names. If they enter "B", then I'd like to be able to offer "Bayshore", "Babylon" and "Bohemia" as suggestions, and so on.

A completely legit application.

I am under the assumption that there currently does not exist a QuickType API.

Am I wrong?


回答1:


There is no QuickType API but you can get similar functionality with the ACEAutocompleteBar library from GitHub. Just for the record, I have listed the steps to get this working with Swift:

1) Import the files from the folder "ACEAutocompleteBar" to your project.

2) Create a bridging header and write #import "ACEAutocompleteBar.h" at the top.

3) Make your view containing the text field an ACEAutocompleteDataSource and ACEAutocompleteDelegate.

4) Implement the minimumCharactersToTrigger function

func minimumCharactersToTrigger(inputView: ACEAutocompleteInputView!) -> UInt {
    return 1
}

5) Implement the inputView function.

func inputView(inputView: ACEAutocompleteInputView!, itemsFor query: String!, result resultBlock: (([AnyObject]!) -> Void)!) {

    inputView.hidden = false
    inputView.alpha = 0.75

    if resultBlock != nil{
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {

        var data:NSMutableArray = []

        if(self.myTextField.isFirstResponder()){    
          //query your data source with 'query' and add any of the results to data to pass back                
        }
        dispatch_async(dispatch_get_main_queue()) {resultBlock(data as [AnyObject])}  
      }
   }
}

6) Implement the textField function.

func textField(textField: UITextField!, didSelectObject object: AnyObject!, inInputView inputView: ACEAutocompleteInputView!) {
    textField.text = String(object)
}

7) Call setAutocompleteWithDataSource on the textField.

self.myTextField.setAutocompleteWithDataSource(self, delegate: self, customize: {

        inputView in

        // customize the view (optional)
        inputView.font = UIFont.systemFontOfSize(20)
        inputView.textColor = UIColor.whiteColor()
        inputView.backgroundColor = UIColor.blueColor()
        inputView.hidden = false
    })

Hope that helps anyone looking for this kind of functionality!



来源:https://stackoverflow.com/questions/31167416/customizing-quicktype-suggestions-in-ios

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!