OOContactUnitHeader.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // OOContactUnitHeader.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/11/21.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import UIKit
  9. import Hue
  10. class OOContactUnitHeader: UIView {
  11. private let firstWords = "通讯录>"
  12. @IBOutlet weak var containerView: UIScrollView!
  13. override init(frame: CGRect) {
  14. super.init(frame: frame)
  15. }
  16. required init?(coder aDecoder: NSCoder) {
  17. super.init(coder: aDecoder)
  18. }
  19. //载入会执行
  20. override func awakeFromNib() {
  21. }
  22. func setNavBar(_ level:Int,_ levelName:String?){
  23. //增加第一个Label
  24. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor(hex:"#333333"),NSAttributedString.Key.font:UIFont(name:"PingFangSC-Regular",size:15)!]
  25. let firstSize = firstWords.getSize(with: 15)
  26. var oX = CGFloat(4.0)
  27. let oY = (containerView.bounds.height - firstSize.height) / 2
  28. let firstLabel = UILabel(frame: CGRect(x: CGFloat(oX), y: oY, width: firstSize.width, height: firstSize.height))
  29. firstLabel.textAlignment = .left
  30. firstLabel.attributedText = NSMutableAttributedString(string: firstWords, attributes: textAttributes)
  31. firstLabel.sizeToFit()
  32. oX += firstSize.width
  33. containerView.addSubview(firstLabel)
  34. guard let lName = levelName else {
  35. return
  36. }
  37. if level >= 1 {
  38. let theWords = lName.split(separator: "/")
  39. let lastWord = theWords.last
  40. theWords.forEach({ (word) in
  41. var title = ""
  42. if word == lastWord {
  43. title = String(word)
  44. }else{
  45. title = String(word)+">"
  46. }
  47. let wordSize = title.getSize(with: 15)
  48. let wordLabel = UILabel(frame: CGRect(x: oX, y: oY, width: wordSize.width, height: wordSize.height))
  49. wordLabel.textAlignment = .left
  50. wordLabel.attributedText = NSMutableAttributedString(string: title,attributes: textAttributes)
  51. wordLabel.sizeToFit()
  52. oX += wordSize.width
  53. containerView.addSubview(wordLabel)
  54. })
  55. }
  56. }
  57. }