|
| 1 | +// |
| 2 | +// ICToken.swift |
| 3 | +// iCook |
| 4 | +// |
| 5 | +// Created by Ben on 03/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 | + |
| 29 | +class ICToken: UIView { |
| 30 | + |
| 31 | + var text = "" { |
| 32 | + didSet { |
| 33 | + updateTextLabel() |
| 34 | + frame = CGRect(origin: CGPoint.zero, size: systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + var highlighted = false { |
| 39 | + didSet { |
| 40 | + updateTextLabel() |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + var normalTextAttributes: [String : NSObject] = [ |
| 45 | + NSForegroundColorAttributeName: UIColor.whiteColor(), |
| 46 | + NSBackgroundColorAttributeName: UIColor.whiteColor().colorWithAlphaComponent(0.25) |
| 47 | + ] |
| 48 | + |
| 49 | + var highlightedTextAttributes: [String: NSObject] = [ |
| 50 | + NSForegroundColorAttributeName: UIColor(red:0.8, green:0.32, blue:0.24, alpha:1), |
| 51 | + NSBackgroundColorAttributeName: UIColor.whiteColor() |
| 52 | + ] |
| 53 | + |
| 54 | + // MARK: - Private Properties |
| 55 | + |
| 56 | + private(set) lazy var delimiterLabel: UILabel = { |
| 57 | + let _delimiter = UILabel() |
| 58 | + _delimiter.text = " , " |
| 59 | + _delimiter.textColor = UIColor.whiteColor() |
| 60 | + return _delimiter |
| 61 | + }() |
| 62 | + |
| 63 | + private(set) lazy var textLabel: UILabel = { |
| 64 | + let _label = ICInsetLabel(contentEdgeInsets: UIEdgeInsets(top: 3, left: 5, bottom: 3, right: 5), cornerRadius: .Constant(3)) |
| 65 | + _label.textAlignment = .Center |
| 66 | + _label.textColor = self.normalTextAttributes[NSForegroundColorAttributeName] as? UIColor |
| 67 | + _label.backgroundColor = self.normalTextAttributes[NSBackgroundColorAttributeName] as? UIColor |
| 68 | + _label.numberOfLines = 1 |
| 69 | + return _label |
| 70 | + }() |
| 71 | + |
| 72 | + // MARK: - Initialization |
| 73 | + |
| 74 | + override init(frame: CGRect) { |
| 75 | + super.init(frame: frame) |
| 76 | + setUpSubviews() |
| 77 | + } |
| 78 | + |
| 79 | + required init?(coder aDecoder: NSCoder) { |
| 80 | + super.init(coder: aDecoder) |
| 81 | + setUpSubviews() |
| 82 | + } |
| 83 | + |
| 84 | + convenience init(text: String) { |
| 85 | + self.init() |
| 86 | + // didSet is not called within the initializer |
| 87 | + setText(text) |
| 88 | + } |
| 89 | + |
| 90 | + private func setText(text: String) { |
| 91 | + self.text = text |
| 92 | + } |
| 93 | + |
| 94 | + // MARK: - Private Methods |
| 95 | + |
| 96 | + private func updateTextLabel() { |
| 97 | + var attributes = highlighted ? highlightedTextAttributes : normalTextAttributes |
| 98 | + if let color = attributes[NSBackgroundColorAttributeName] as? UIColor { |
| 99 | + textLabel.backgroundColor = color |
| 100 | + } |
| 101 | + attributes[NSBackgroundColorAttributeName] = nil |
| 102 | + textLabel.attributedText = NSAttributedString(string: text, attributes: attributes) |
| 103 | + } |
| 104 | + |
| 105 | + private func setUpSubviews() { |
| 106 | + addSubview(textLabel) |
| 107 | + addSubview(delimiterLabel) |
| 108 | + textLabel.translatesAutoresizingMaskIntoConstraints = false |
| 109 | + delimiterLabel.translatesAutoresizingMaskIntoConstraints = false |
| 110 | + |
| 111 | + let views = [ |
| 112 | + "text": textLabel, |
| 113 | + "delimiter": delimiterLabel |
| 114 | + ] |
| 115 | + addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[text][delimiter]|", |
| 116 | + options: [.AlignAllCenterY], |
| 117 | + metrics: nil, |
| 118 | + views: views |
| 119 | + )) |
| 120 | + addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-2-[text]-2-|", |
| 121 | + options: [], |
| 122 | + metrics: nil, |
| 123 | + views: views |
| 124 | + )) |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments