SAIToolboxItemView.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // SAIToolboxItemView.swift
  3. // SAC
  4. //
  5. // Created by SAGESSE on 9/15/16.
  6. // Copyright © 2016-2017 SAGESSE. All rights reserved.
  7. //
  8. import UIKit
  9. internal class SAIToolboxItemView: UICollectionViewCell {
  10. var item: SAIToolboxItem? {
  11. didSet {
  12. _titleLabel.text = item?.name
  13. _iconView.image = item?.image
  14. _iconView.highlightedImage = item?.highlightedImage
  15. }
  16. }
  17. weak var handler: AnyObject?
  18. private func _init() {
  19. _titleLabel.font = UIFont.systemFont(ofSize: 12)
  20. _titleLabel.textColor = .gray
  21. _titleLabel.textAlignment = .center
  22. _titleLabel.translatesAutoresizingMaskIntoConstraints = false
  23. _iconView.contentMode = .scaleAspectFit
  24. _iconView.translatesAutoresizingMaskIntoConstraints = false
  25. let view = UIView()
  26. view.backgroundColor = UIColor(white: 0, alpha: 0.2)
  27. view.layer.cornerRadius = 4
  28. selectedBackgroundView = view
  29. contentView.addSubview(_iconView)
  30. contentView.addSubview(_titleLabel)
  31. addConstraints([
  32. _SAInputLayoutConstraintMake(_iconView, .centerX, .equal, self, .centerX),
  33. _SAInputLayoutConstraintMake(_iconView, .centerY, .equal, self, .centerY, -12),
  34. _SAInputLayoutConstraintMake(_iconView, .width, .equal, nil, .notAnAttribute, 43),
  35. _SAInputLayoutConstraintMake(_iconView, .height, .equal, nil, .notAnAttribute, 43),
  36. _SAInputLayoutConstraintMake(_titleLabel, .top, .equal, _iconView, .bottom, 4),
  37. _SAInputLayoutConstraintMake(_titleLabel, .height, .equal, nil, .notAnAttribute, 20),
  38. _SAInputLayoutConstraintMake(_titleLabel, .centerX, .equal, self, .centerX),
  39. ])
  40. }
  41. private lazy var _iconView: UIImageView = UIImageView()
  42. private lazy var _titleLabel: UILabel = UILabel()
  43. override init(frame: CGRect) {
  44. super.init(frame: frame)
  45. _init()
  46. }
  47. required init?(coder aDecoder: NSCoder) {
  48. super.init(coder: aDecoder)
  49. _init()
  50. }
  51. }