IHAreaItem.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // IHAreaItem.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2019/12/26.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. fileprivate class IHAreaItemButton: UIButton {
  10. override func layoutSubviews() {
  11. super.layoutSubviews()
  12. let titleLabelFrame = titleLabel?.frame
  13. let imageViewFrame = imageView?.frame
  14. let totleWidth = (titleLabelFrame?.size.width)! + (imageViewFrame?.size.width)! + 2
  15. titleLabel?.frame.origin.x = ( self.frame.size.width - totleWidth ) / 2
  16. imageView?.frame.origin.x = (titleLabel?.frame.origin.x)! + (titleLabelFrame?.size.width)! + 2
  17. }
  18. }
  19. class IHAreaItem: UIView {
  20. var defaultDesc:String?{
  21. didSet{
  22. if let desc = self.defaultDesc {
  23. button.setTitle(desc, for: .normal)
  24. }
  25. }
  26. }
  27. var titleArray :[String]?{
  28. didSet{
  29. buttonTitle = self.titleArray?.first
  30. }
  31. }
  32. var confirmBlock:((Int)->Void)?
  33. private var buttonTitle : String?{
  34. didSet{
  35. if let buttonTitle = self.buttonTitle {
  36. button.setTitle(buttonTitle, for: .normal)
  37. }
  38. }
  39. }
  40. private lazy var button: IHAreaItemButton = {
  41. let btn = IHAreaItemButton(type: .custom)
  42. btn.setImage(UIImage(named: "itemxiala"), for: .normal)
  43. btn.setTitle("Building", for: .normal)
  44. btn.setTitleColor(UIColor(hexString: "333333"), for: .normal)
  45. btn.titleLabel?.font = UIFont(name: PingFangSC_Semibold, size: 13)
  46. btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
  47. return btn
  48. }()
  49. override init(frame: CGRect) {
  50. super.init(frame: frame)
  51. self.backgroundColor = UIColor(hexString: "#F6F8FA")
  52. createUI()
  53. }
  54. required init?(coder: NSCoder) {
  55. fatalError("init(coder:) has not been implemented")
  56. }
  57. @objc private func btnClick(sennder: UIButton) {
  58. if self.titleArray?.count == 0 {
  59. g_showHUD("无数据!")
  60. button.setTitle("Building", for: .normal)
  61. return
  62. }
  63. let pick = THScrollChooseView(question: self.titleArray, withDefaultDesc: self.defaultDesc ?? self.titleArray?.first)
  64. pick?.confirmBlock = {(selectedIndex) in
  65. log.debug(" title = \(self.titleArray?[selectedIndex])")
  66. let title = self.titleArray?[selectedIndex]
  67. self.buttonTitle = title
  68. if let confirm = self.confirmBlock {
  69. confirm(selectedIndex)
  70. }
  71. }
  72. pick?.show()
  73. }
  74. func createUI() {
  75. let line = UIView(frame: CGRect(x: self.width - 1, y: 8, width: 1, height: 17))
  76. line.backgroundColor = UIColor(hexString: "E2E2E2")
  77. addSubview(line)
  78. button.frame = CGRect(x: 0, y: 0, width: self.width - 1, height: self.height)
  79. addSubview(button)
  80. }
  81. }