ContactItemCell.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // ContactItemCell.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 16/7/12.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. protocol ContactItemCellBreadcrumbClickDelegate {
  10. func breadcrumbTap(name: String, distinguished: String)
  11. }
  12. class ContactItemCell: UITableViewCell {
  13. @IBOutlet weak var iconImageView: UIImageView!
  14. @IBOutlet weak var nameLabel: UILabel!
  15. @IBOutlet weak var iconTagLabel: UILabel!
  16. // 面包屑导航栏
  17. @IBOutlet weak var headBarScrollView: UIScrollView!
  18. var delegate: ContactItemCellBreadcrumbClickDelegate?
  19. var cellViewModel:CellViewModel? {
  20. didSet {
  21. self.configCellViewModel()
  22. }
  23. }
  24. override func awakeFromNib() {
  25. }
  26. override func setSelected(_ selected: Bool, animated: Bool) {
  27. super.setSelected(selected, animated: animated)
  28. }
  29. func configCellViewModel() {
  30. let dataType = cellViewModel?.dataType
  31. switch dataType! {
  32. case .company(_):
  33. self.configCompany()
  34. break
  35. case .depart(_):
  36. self.configDepart()
  37. break
  38. case .group(_):
  39. self.configGroup()
  40. break
  41. case .person(let p):
  42. self.configPerson(p as! PersonV2)
  43. break
  44. case .identity(let i):
  45. self.configIdentity(i as! IdentityV2)
  46. case .title(let t):
  47. self.configTitle(t as! HeadTitle)
  48. break
  49. }
  50. }
  51. private func configCompany(){
  52. self.nameLabel.text = cellViewModel?.name
  53. self.iconTagLabel.isHidden = true
  54. self.iconImageView.image = O2ThemeManager.image(for: "Icon.icon_company")
  55. self.iconImageView.layer.masksToBounds = true
  56. self.iconImageView.layer.cornerRadius = self.iconImageView.width / 2.0
  57. }
  58. private func configDepart() {
  59. self.nameLabel.text = cellViewModel?.name
  60. self.iconTagLabel.isHidden = false
  61. self.iconImageView.image = O2ThemeManager.image(for: "Icon.pic_oval")
  62. self.iconImageView.layer.masksToBounds = true
  63. self.iconImageView.layer.cornerRadius = 40 / 2.0
  64. if let orgName = cellViewModel?.name,orgName != "" {
  65. self.iconTagLabel.text = NSString(string: orgName).substring(to: 1)
  66. self.iconTagLabel.center = iconImageView.center
  67. }
  68. self.nameLabel.font = UIFont(name: "PingFang SC", size: 14)
  69. }
  70. private func configGroup() {
  71. self.nameLabel.text = cellViewModel?.name
  72. self.iconTagLabel.isHidden = true
  73. self.iconImageView.image = O2ThemeManager.image(for: "Icon.icon_group")
  74. self.iconImageView.layer.masksToBounds = true
  75. self.iconImageView.layer.cornerRadius = self.iconImageView.width / 2.0
  76. }
  77. private func configPerson(_ p: PersonV2) {
  78. self.nameLabel.text = cellViewModel?.name
  79. self.iconTagLabel.isHidden = true
  80. self.iconImageView.layer.masksToBounds = true
  81. self.iconImageView.layer.cornerRadius = self.iconImageView.width / 2.0
  82. let person = p
  83. let urlstr = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKeyV2, query: ContactContext.personIconByNameQueryV2, parameter: ["##name##":person.id as AnyObject], generateTime: false)
  84. let url = URL(string: urlstr!)
  85. self.iconImageView.hnk_setImageFromURL(url!)
  86. }
  87. private func configIdentity(_ id: IdentityV2) {
  88. self.nameLabel.text = cellViewModel?.name
  89. self.iconTagLabel.isHidden = true
  90. self.iconImageView.layer.masksToBounds = true
  91. self.iconImageView.layer.cornerRadius = self.iconImageView.width / 2.0
  92. let identity = id
  93. let urlstr = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKeyV2, query: ContactContext.personIconByNameQueryV2, parameter: ["##name##":identity.person as AnyObject], generateTime: false)
  94. let url = URL(string: urlstr!)
  95. self.iconImageView.hnk_setImageFromURL(url!)
  96. }
  97. private func configTitle(_ t: HeadTitle) {
  98. let title = t
  99. if title.isBar {
  100. self.headBarScrollView.removeSubviews()
  101. var oX = CGFloat(4.0)
  102. if let bars = title.barText {
  103. bars.forEachEnumerated { (index, bar) in
  104. var name: String
  105. var textColor:UIColor
  106. if bars.count == (index+1) {
  107. name = bar.name ?? ""
  108. textColor = base_color
  109. }else {
  110. name = bar.name ?? ""
  111. name = name + " > "
  112. textColor = UIColor(hex:"#333333")
  113. }
  114. let firstSize = name.getSize(with: 15)
  115. let oY = (self.headBarScrollView.bounds.height - firstSize.height) / 2
  116. let firstLabel = UILabel(frame: CGRect(x: CGFloat(oX), y: oY, width: firstSize.width, height: firstSize.height))
  117. firstLabel.textAlignment = .left
  118. let textAttributes = [NSAttributedString.Key.foregroundColor: textColor,NSAttributedString.Key.font:UIFont(name:"PingFangSC-Regular",size:15)!]
  119. firstLabel.attributedText = NSMutableAttributedString(string: name, attributes: textAttributes)
  120. firstLabel.sizeToFit()
  121. oX += firstSize.width
  122. self.headBarScrollView.addSubview(firstLabel)
  123. firstLabel.addTapGesture(action: { (rec) in
  124. if bars.count != (index+1) {
  125. self.delegate?.breadcrumbTap(name: bar.name ?? "", distinguished: bar.distinguishedName ?? "")
  126. }
  127. })
  128. }
  129. }
  130. }else{
  131. nameLabel.text = cellViewModel?.name
  132. iconTagLabel.isHidden = true
  133. iconImageView.image = UIImage(named: title.icon!)
  134. // iconImageView.frame = CGRect(x: 8, y: 10, width: 30, height: 30)
  135. iconImageView.layer.masksToBounds = true
  136. iconImageView.layer.cornerRadius = 15.0
  137. nameLabel.font = UIFont(name: "PingFang SC", size: 16.0)
  138. // nameLabel.frame.x = 66
  139. }
  140. }
  141. }