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