AKExcelCollectionViewCell.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // AKExcelCollectionViewCell.swift
  3. // YunYingSwift
  4. //
  5. // Created by AlasKu on 17/2/10.
  6. // Copyright © 2017年 innostic. All rights reserved.
  7. //
  8. import UIKit
  9. class AKExcelCollectionViewCell: UICollectionViewCell {
  10. var horizontalMargin : CGFloat? {
  11. didSet{
  12. setupFrame()
  13. }
  14. }
  15. var customView : UIView? {
  16. didSet{
  17. guard let customView = customView else { return }
  18. customView.frame = bounds
  19. contentView.insertSubview(customView, at: 0)
  20. textLabel.text = ""
  21. }
  22. }
  23. lazy var textLabel : UILabel = {
  24. let label = UILabel()
  25. label.numberOfLines = 0
  26. label.textAlignment = .center
  27. label.backgroundColor = UIColor.clear
  28. return label
  29. }()
  30. private lazy var separatorLayer : CAShapeLayer = {
  31. let lay = CAShapeLayer.init()
  32. lay.strokeColor = UIColorFromHex(rgbValue: 0x000, alpha: 0.8).cgColor
  33. lay.lineWidth = 0.5
  34. return lay
  35. }()
  36. override init(frame: CGRect) {
  37. super.init(frame: frame)
  38. setup()
  39. }
  40. required init?(coder aDecoder: NSCoder) {
  41. fatalError("init(coder:) has not been implemented")
  42. }
  43. func setup() {
  44. addSubview(textLabel)
  45. contentView.layer.addSublayer(separatorLayer)
  46. }
  47. override func layoutSubviews() {
  48. super.layoutSubviews()
  49. setupFrame()
  50. }
  51. func setupFrame() {
  52. let newFrame = CGRect(x: horizontalMargin!, y: 0, width: bounds.width - 2 * horizontalMargin!, height: bounds.height)
  53. textLabel.frame = newFrame
  54. let path = UIBezierPath()
  55. path.move(to: CGPoint(x: bounds.width - 0.5, y: 0))
  56. path.addLine(to: CGPoint(x: bounds.width - 0.5, y: bounds.height))
  57. path.move(to: CGPoint(x: 0, y: bounds.height - 0.5))
  58. path.addLine(to: CGPoint(x: bounds.width, y: bounds.height - 0.5))
  59. separatorLayer.path = path.cgPath
  60. }
  61. override func prepareForReuse() {
  62. super.prepareForReuse()
  63. customView?.removeFromSuperview()
  64. }
  65. }