|
| 1 | +// |
| 2 | +// CustomizedTokenField.swift |
| 3 | +// Example |
| 4 | +// |
| 5 | +// Created by Ben on 09/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 CustomizedTokenField: UIView { |
| 31 | + |
| 32 | + override init(frame: CGRect) { |
| 33 | + super.init(frame: frame) |
| 34 | + setUpSubviews() |
| 35 | + } |
| 36 | + |
| 37 | + required init?(coder aDecoder: NSCoder) { |
| 38 | + super.init(coder: aDecoder) |
| 39 | + setUpSubviews() |
| 40 | + } |
| 41 | + |
| 42 | + private func setUpSubviews() { |
| 43 | + backgroundColor = UIColor(red:0.96, green:0.48, blue:0.4, alpha:1) |
| 44 | + |
| 45 | + let tokenField = ICTokenField() |
| 46 | + tokenField.applyCustomizedStyle() |
| 47 | + tokenField.translatesAutoresizingMaskIntoConstraints = false |
| 48 | + addSubview(tokenField) |
| 49 | + |
| 50 | + let views = ["field": tokenField] |
| 51 | + addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[field]-10-|", options: [], metrics: nil, views: views)) |
| 52 | + addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-3-[field]-3-|", options: [], metrics: nil, views: views)) |
| 53 | + } |
| 54 | + |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +//////////////////////////////////////////////////////////////////////////////// |
| 59 | + |
| 60 | + |
| 61 | +extension ICTokenField { |
| 62 | + |
| 63 | + func applyCustomizedStyle() { |
| 64 | + layer.cornerRadius = 5 |
| 65 | + layer.shouldRasterize = true |
| 66 | + layer.rasterizationScale = UIScreen.mainScreen().scale |
| 67 | + backgroundColor = UIColor(red:0.8, green:0.32, blue:0.24, alpha:1) |
| 68 | + |
| 69 | + textField.textColor = UIColor.whiteColor() |
| 70 | + textField.tintColor = UIColor.whiteColor() |
| 71 | + textField.font = UIFont.boldSystemFontOfSize(14) |
| 72 | + |
| 73 | + attributedPlaceholder = NSAttributedString( |
| 74 | + string: String(self.dynamicType), |
| 75 | + attributes: [ |
| 76 | + NSForegroundColorAttributeName: UIColor.whiteColor().colorWithAlphaComponent(0.5), |
| 77 | + NSFontAttributeName: UIFont.boldSystemFontOfSize(14) |
| 78 | + ] |
| 79 | + ) |
| 80 | + |
| 81 | + normalTokenAttributes = [ |
| 82 | + NSForegroundColorAttributeName: UIColor.whiteColor(), |
| 83 | + NSBackgroundColorAttributeName: UIColor.whiteColor().colorWithAlphaComponent(0.25), |
| 84 | + NSFontAttributeName: UIFont.boldSystemFontOfSize(14) |
| 85 | + ] |
| 86 | + |
| 87 | + highlightedTokenAttributes = [ |
| 88 | + NSForegroundColorAttributeName: UIColor(red:0.8, green:0.32, blue:0.24, alpha:1), |
| 89 | + NSBackgroundColorAttributeName: UIColor.whiteColor(), |
| 90 | + NSFontAttributeName: UIFont.boldSystemFontOfSize(14) |
| 91 | + ] |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments