|
| 1 | +// |
| 2 | +// CustomizedTokenViewController.swift |
| 3 | +// Example |
| 4 | +// |
| 5 | +// Created by Ben on 11/03/2016. |
| 6 | +// Copyright © 2016 Polydice, Inc. |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +// of this software and associated documentation files (the "Software"), to deal |
| 10 | +// in the Software without restriction, including without limitation the rights |
| 11 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +// copies of the Software, and to permit persons to whom the Software is |
| 13 | +// furnished to do so, subject to the following conditions: |
| 14 | +// |
| 15 | +// The above copyright notice and this permission notice shall be included in all |
| 16 | +// copies or substantial portions of the Software. |
| 17 | +// |
| 18 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | +// SOFTWARE. |
| 25 | +// |
| 26 | + |
| 27 | +import UIKit |
| 28 | +import ICInputAccessory |
| 29 | + |
| 30 | +class CustomizedTokenViewController: UIViewController, ICTokenFieldDelegate { |
| 31 | + |
| 32 | + private let tokenField = CustomizedTokenField() |
| 33 | + private let textView = UITextView() |
| 34 | + |
| 35 | + // MARK: - UIViewController |
| 36 | + |
| 37 | + override func loadView() { |
| 38 | + super.loadView() |
| 39 | + view.backgroundColor = UIColor.whiteColor() |
| 40 | + textView.text = "[\n\n]"; |
| 41 | + textView.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline) |
| 42 | + textView.frame = view.bounds.insetBy(dx: 10, dy: 10) |
| 43 | + textView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] |
| 44 | + view.addSubview(textView) |
| 45 | + } |
| 46 | + |
| 47 | + override func viewDidLoad() { |
| 48 | + super.viewDidLoad() |
| 49 | + navigationController?.navigationBar.barTintColor = UIColor(red:0.96, green:0.48, blue:0.4, alpha:1) |
| 50 | + navigationController?.navigationBar.translucent = false |
| 51 | + navigationController?.navigationBar.barStyle = .Black |
| 52 | + |
| 53 | + let cancelBarButton = UIBarButtonItem(barButtonSystemItem: .Cancel, target: self, action: Selector("dismiss:")) |
| 54 | + cancelBarButton.tintColor = UIColor.whiteColor() |
| 55 | + navigationItem.rightBarButtonItem = cancelBarButton |
| 56 | + |
| 57 | + navigationItem.titleView = tokenField |
| 58 | + tokenField.delegate = self |
| 59 | + } |
| 60 | + |
| 61 | + override func viewWillAppear(animated: Bool) { |
| 62 | + super.viewWillAppear(animated) |
| 63 | + tokenField.becomeFirstResponder() |
| 64 | + } |
| 65 | + |
| 66 | + override func viewWillDisappear(animated: Bool) { |
| 67 | + super.viewWillAppear(animated) |
| 68 | + tokenField.resignFirstResponder() |
| 69 | + textView.endEditing(true) |
| 70 | + } |
| 71 | + |
| 72 | + // MARK: - ICTokenFieldDelegate |
| 73 | + |
| 74 | + func tokenFieldDidBeginEditing(tokenField: ICTokenField) { |
| 75 | + print(__FUNCTION__) |
| 76 | + } |
| 77 | + |
| 78 | + func tokenFieldDidEndEditing(tokenField: ICTokenField) { |
| 79 | + print(__FUNCTION__) |
| 80 | + } |
| 81 | + |
| 82 | + func tokenFieldWillReturn(tokenField: ICTokenField) { |
| 83 | + print(__FUNCTION__) |
| 84 | + } |
| 85 | + |
| 86 | + func tokenField(tokenField: ICTokenField, didEnterText text: String) { |
| 87 | + print("Add: \"\(text)\"") |
| 88 | + updateTexts() |
| 89 | + } |
| 90 | + |
| 91 | + func tokenField(tokenField: ICTokenField, didDeleteText text: String, atIndex index: Int) { |
| 92 | + print("Delete: \"\(text)\"") |
| 93 | + updateTexts() |
| 94 | + } |
| 95 | + |
| 96 | + // MARK: - UIResponder Callbacks |
| 97 | + |
| 98 | + @IBAction private func dismiss(sender: UIBarButtonItem) { |
| 99 | + presentingViewController?.dismissViewControllerAnimated(true, completion: nil) |
| 100 | + } |
| 101 | + |
| 102 | + // MARK: - Private Methods |
| 103 | + |
| 104 | + private func updateTexts() { |
| 105 | + textView.text = "[\n " + tokenField.texts.map { "\"" + $0 + "\"" } .joinWithSeparator(",\n ") + "\n]" |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments