2

I want to create custom tokenizer for @ as in WhatsApp feature when you open group and type @ then there is a popup displaying list of friends and you select one from the list.

I want to show something like this:

enter image description here

johnrao07
  • 6,690
  • 4
  • 32
  • 55
  • this question has been asked for "android" here http://stackoverflow.com/questions/40460997/android-multiautocompletetextview-with-custom-tokenizer-like-as-whatsapp-groupch – johnrao07 Nov 07 '16 at 12:54
  • use textField delegate method `shouldChangeCharactersInRange` and `tableView`, detect every character when @ appears show `tableView` with list. – vaibhav Nov 07 '16 at 13:12

1 Answers1

3

Some demo code workout i have done previously.

Demo Slide Up Like @

Github post

Hope this will help you.

import UIKit

class ViewController: UIViewController {

    @IBOutlet var txtSearch : UITextField!
    @IBOutlet var tblUserData : UITableView!
    var myArrayOfDict: NSMutableArray = []

    override func viewDidLoad() {
        super.viewDidLoad()

        myArrayOfDict = [["UserName": "Hasya", "UserID": "1", "UserImage": "1.png"],["UserName": "Demo", "UserID": "2", "UserImage": "2.png"],["UserName": "Hasya & Hasya", "UserID": "3", "UserImage": "3.png"],["UserName": "Demo User", "UserID": "4", "UserImage": "4.png"]]

        tblUserData.layer.borderColor = UIColor.blueColor().CGColor
        tblUserData.layer.borderWidth = 1.0
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return myArrayOfDict.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {

        let cell : CustomCell! = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath)  as! CustomCell

        cell.imgUser.layer.cornerRadius = cell.imgUser.frame.size.height/2

        let strImageName : String = (myArrayOfDict.objectAtIndex(indexPath.row).valueForKey("UserImage") as? String)!

        cell.imgUser.image = UIImage(named: (strImageName))

        cell.lblName.text = myArrayOfDict.objectAtIndex(indexPath.row).valueForKey("UserName") as? String


        return cell
    }


    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {


        let str : String = (myArrayOfDict.objectAtIndex(indexPath.row).valueForKey("UserName") as? String)!

        txtSearch.text = txtSearch.text! + str
        // String(format:"@", myArrayOfDict.objectAtIndex(indexPath.row).valueForKey("UserName") as? String)

        UIView.animateWithDuration(0.5) { () -> Void in

            var frameOftblUserData : CGRect = self.tblUserData.frame
            frameOftblUserData.origin.y = 264
            frameOftblUserData.size.height = 0
            self.tblUserData.frame = frameOftblUserData

        }


    }

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

        print(string)

        if string.hasPrefix("@")
        {
            print("YES")

            UIView.animateWithDuration(0.5) { () -> Void in

                var frameOftblUserData : CGRect = self.tblUserData.frame
                frameOftblUserData.origin.y = 132
                frameOftblUserData.size.height = 132
                self.tblUserData.frame = frameOftblUserData

            }

        }
        else
        {
            print("NO")

            UIView.animateWithDuration(0.5) { () -> Void in

                var frameOftblUserData : CGRect = self.tblUserData.frame
                frameOftblUserData.origin.y = 264
                frameOftblUserData.size.height = 0
                self.tblUserData.frame = frameOftblUserData

            }

        }

        return true;
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

enter image description here

Hasya
  • 9,792
  • 4
  • 31
  • 46
  • 1
    exactly what i was suggested using comment, +1 bro. – vaibhav Nov 07 '16 at 13:19
  • thanks for answering, there is one issue, when the cursor position reaches #tagvalue, we should remove whole #tagvalue value instead of each letter. Is there a way to go about it? – johnrao07 Nov 08 '16 at 07:33