ContactItemCell.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. override var frame: CGRect {
  30. didSet {
  31. var newFrame = frame
  32. newFrame.origin.x += 10
  33. newFrame.size.width -= 20
  34. super.frame = newFrame
  35. }
  36. }
  37. func configCellViewModel() {
  38. let dataType = cellViewModel?.dataType
  39. switch dataType! {
  40. case .company(_):
  41. self.configCompany()
  42. break
  43. case .depart(_):
  44. self.configDepart()
  45. break
  46. case .group(_):
  47. self.configGroup()
  48. break
  49. case .person(let p):
  50. self.configPerson(p as! PersonV2)
  51. break
  52. case .identity(let i):
  53. self.configIdentity(i as! IdentityV2)
  54. case .title(let t):
  55. self.configTitle(t as! HeadTitle)
  56. break
  57. }
  58. }
  59. private func configCompany(){
  60. self.nameLabel.text = cellViewModel?.name
  61. self.iconTagLabel.isHidden = true
  62. self.iconImageView.image = O2ThemeManager.image(for: "Icon.icon_company")
  63. self.iconImageView.layer.masksToBounds = true
  64. self.iconImageView.layer.cornerRadius = self.iconImageView.width / 2.0
  65. }
  66. private func configDepart() {
  67. self.nameLabel.text = cellViewModel?.name
  68. self.iconTagLabel.isHidden = false
  69. self.iconImageView.image = O2ThemeManager.image(for: "Icon.pic_oval")
  70. self.iconImageView.layer.masksToBounds = true
  71. self.iconImageView.layer.cornerRadius = 40 / 2.0
  72. if let orgName = cellViewModel?.name,orgName != "" {
  73. self.iconTagLabel.text = NSString(string: orgName).substring(to: 1)
  74. self.iconTagLabel.center = iconImageView.center
  75. }
  76. self.nameLabel.font = UIFont(name: "PingFang SC", size: 14)
  77. }
  78. private func configGroup() {
  79. self.nameLabel.text = cellViewModel?.name
  80. self.iconTagLabel.isHidden = true
  81. self.iconImageView.image = O2ThemeManager.image(for: "Icon.icon_group")
  82. self.iconImageView.layer.masksToBounds = true
  83. self.iconImageView.layer.cornerRadius = self.iconImageView.width / 2.0
  84. }
  85. private func configPerson(_ p: PersonV2) {
  86. self.nameLabel.text = cellViewModel?.name
  87. self.iconTagLabel.isHidden = true
  88. self.iconImageView.layer.masksToBounds = true
  89. self.iconImageView.layer.cornerRadius = self.iconImageView.width / 2.0
  90. let person = p
  91. let urlstr = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKeyV2, query: ContactContext.personIconByNameQueryV2, parameter: ["##name##":person.id as AnyObject], generateTime: false)
  92. let url = URL(string: urlstr!)
  93. self.iconImageView.hnk_setImageFromURL(url!)
  94. }
  95. private func configIdentity(_ id: IdentityV2) {
  96. self.nameLabel.text = cellViewModel?.name
  97. self.iconTagLabel.isHidden = true
  98. self.iconImageView.layer.masksToBounds = true
  99. self.iconImageView.layer.cornerRadius = self.iconImageView.width / 2.0
  100. let identity = id
  101. let urlstr = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKeyV2, query: ContactContext.personIconByNameQueryV2, parameter: ["##name##":identity.person as AnyObject], generateTime: false)
  102. let url = URL(string: urlstr!)
  103. self.iconImageView.hnk_setImageFromURL(url!)
  104. }
  105. private func configTitle(_ t: HeadTitle) {
  106. let title = t
  107. if title.isBar {
  108. self.headBarScrollView.removeSubviews()
  109. var oX = CGFloat(4.0)
  110. if let bars = title.barText {
  111. bars.forEachEnumerated { (index, bar) in
  112. var name: String
  113. var textColor:UIColor
  114. if bars.count == (index+1) {
  115. name = bar.name ?? ""
  116. textColor = base_color
  117. }else {
  118. name = bar.name ?? ""
  119. name = name + " > "
  120. textColor = UIColor(hex:"#333333")
  121. }
  122. let firstSize = name.getSize(with: 15)
  123. let oY = (self.headBarScrollView.bounds.height - firstSize.height) / 2
  124. let firstLabel = UILabel(frame: CGRect(x: CGFloat(oX), y: oY, width: firstSize.width, height: firstSize.height))
  125. firstLabel.textAlignment = .left
  126. let textAttributes = [NSAttributedString.Key.foregroundColor: textColor,NSAttributedString.Key.font:UIFont(name:"PingFangSC-Regular",size:15)!]
  127. firstLabel.attributedText = NSMutableAttributedString(string: name, attributes: textAttributes)
  128. firstLabel.sizeToFit()
  129. oX += firstSize.width
  130. self.headBarScrollView.addSubview(firstLabel)
  131. firstLabel.addTapGesture(action: { (rec) in
  132. if bars.count != (index+1) {
  133. self.delegate?.breadcrumbTap(name: bar.name ?? "", distinguished: bar.distinguishedName ?? "")
  134. }
  135. })
  136. }
  137. }
  138. }else{
  139. nameLabel.text = cellViewModel?.name
  140. iconTagLabel.isHidden = true
  141. iconImageView.image = UIImage(named: title.icon!)
  142. // iconImageView.frame = CGRect(x: 8, y: 10, width: 30, height: 30)
  143. iconImageView.layer.masksToBounds = true
  144. iconImageView.layer.cornerRadius = 15.0
  145. nameLabel.font = UIFont(name: "PingFang SC", size: 16.0)
  146. // nameLabel.frame.x = 66
  147. }
  148. }
  149. }