UnitBreadcrumbViewCell.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // UnitBreadcrumbViewCell.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/8/13.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. protocol UnitPickerBreadcrumbClickDelegate {
  11. func breadcrumbTap(name: String, distinguished: String)
  12. }
  13. class UnitBreadcrumbViewCell: UITableViewCell {
  14. @IBOutlet weak var breadcrumbScrollView: UIScrollView!
  15. var delegate: UnitPickerBreadcrumbClickDelegate?
  16. override func awakeFromNib() {
  17. super.awakeFromNib()
  18. }
  19. override func setSelected(_ selected: Bool, animated: Bool) {
  20. super.setSelected(selected, animated: animated)
  21. }
  22. func refreshBreadcrumb(breadcrumbList: [ContactBreadcrumbBean]) {
  23. if breadcrumbList.count > 0 {
  24. self.breadcrumbScrollView.removeSubviews()
  25. var oX = CGFloat(4.0)
  26. let arrowW = CGFloat(24)
  27. let arrowH = CGFloat(32)
  28. breadcrumbList.forEachEnumerated { (index, bar) in
  29. let name = bar.name
  30. var textColor:UIColor
  31. if breadcrumbList.count == (index+1) {
  32. textColor = UIColor(hex:"#666666")
  33. }else {
  34. textColor = base_color
  35. }
  36. let firstSize = name.getSize(with: 15)
  37. let oY = (self.breadcrumbScrollView.bounds.height - firstSize.height) / 2
  38. let firstLabel = UILabel(frame: CGRect(x: CGFloat(oX), y: oY, width: firstSize.width, height: firstSize.height))
  39. firstLabel.textAlignment = .left
  40. let textAttributes = [NSAttributedString.Key.foregroundColor: textColor,NSAttributedString.Key.font:UIFont(name:"PingFangSC-Regular",size:15)!]
  41. firstLabel.attributedText = NSMutableAttributedString(string: name, attributes: textAttributes)
  42. firstLabel.sizeToFit()
  43. oX += firstSize.width
  44. self.breadcrumbScrollView.addSubview(firstLabel)
  45. if breadcrumbList.count != (index+1) {
  46. let arrowY = (self.breadcrumbScrollView.bounds.height - arrowH) / 2
  47. let arrowImage = UIImageView(frame: CGRect(x: CGFloat(oX), y: arrowY, width: arrowW, height: arrowH))
  48. arrowImage.image = UIImage(named: "arrow_r")
  49. arrowImage.contentMode = .scaleAspectFit
  50. self.breadcrumbScrollView.addSubview(arrowImage)
  51. oX += arrowW
  52. }
  53. firstLabel.addTapGesture(action: { (rec) in
  54. DDLogDebug("点击了 \(index)")
  55. if breadcrumbList.count != (index+1) {
  56. self.delegate?.breadcrumbTap(name: bar.name, distinguished: bar.key)
  57. }
  58. })
  59. }
  60. var size = self.breadcrumbScrollView.contentSize;
  61. size.width = oX;
  62. self.breadcrumbScrollView.showsHorizontalScrollIndicator = true;
  63. self.breadcrumbScrollView.contentSize = size;
  64. self.breadcrumbScrollView.bounces = true;
  65. }
  66. }
  67. }