优惠券swift版马甲包

YMSearchPageHeaderView.swift 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // YMSearchPageHeaderView.swift
  3. // MvpGoods
  4. //
  5. // Created by 小花 on 2019/4/12.
  6. // Copyright © 2019 MVP. All rights reserved.
  7. //
  8. import UIKit
  9. class YMSearchPageHeaderView: UIView {
  10. typealias ClickClosure = (_ text: String) -> Void
  11. typealias ClickSearch = () -> Void
  12. var clickClosure: ClickClosure?
  13. var clickSearch: ClickSearch?
  14. var scrollView: UIScrollView?
  15. var hotArr = [String]()
  16. required init?(coder aDecoder: NSCoder) {
  17. fatalError("init(coder:) has not been implemented")
  18. }
  19. override init(frame: CGRect) {
  20. super.init(frame: frame)
  21. initSubViews()
  22. loadHotData()
  23. }
  24. func initSubViews() {
  25. let searchBg = UIView(frame: CGRect(x: 10, y: 0, width: self.width-20, height: 38))
  26. searchBg.backgroundColor = UIColor.white
  27. searchBg.layer.cornerRadius = 3
  28. self.addSubview(searchBg)
  29. let searchBtn = UIButton(frame: CGRect(x: 10, y: 0, width: searchBg.width, height: searchBg.height))
  30. searchBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
  31. searchBtn.setTitleColor(UIColor.hexadecimalColor(hexadecimal: "#666666"), for: .normal)
  32. searchBtn.set(image: UIImage(named: "search_gray"), title: "输入淘宝天猫商品标题或关键词", titlePosition: .right, additionalSpacing: 5, state: .normal)
  33. searchBg.addSubview(searchBtn)
  34. searchBtn.addTarget(self, action: #selector(clickSearchAction), for: .touchUpInside)
  35. searchBtn.contentHorizontalAlignment = .left
  36. searchBg.addSubview(searchBtn)
  37. let rightBtn = UIButton(frame: CGRect(x: searchBg.width-67, y: 63, width: 63, height: 34))
  38. rightBtn.centerY = searchBg.height/2
  39. rightBtn.setTitle("搜索", for: .normal)
  40. rightBtn.backgroundColor = UIColor.gloadRedColor()
  41. rightBtn.layer.cornerRadius = 3
  42. rightBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
  43. rightBtn.setTitleColor(UIColor.white, for: .normal)
  44. searchBg.addSubview(rightBtn)
  45. let scrollView = UIScrollView(frame: CGRect(x: 10, y: searchBg.bottom, width: self.width-20, height: 74))
  46. scrollView.showsHorizontalScrollIndicator = false
  47. self.addSubview(scrollView)
  48. self.scrollView = scrollView
  49. }
  50. func loadHotData() {
  51. let url = BASE_URL + "/api/v2/channel/hotsearch"
  52. NetWorkTool.getRequest(url: url, param: [:], success: { (json) in
  53. let items = json["rst"]?["data"].arrayObject
  54. for item in items ?? [] {
  55. let str = item as! [String: Any]
  56. let text = str["name"] as! String
  57. self.hotArr.append(text)
  58. }
  59. self.creatLabels(hotArr: self.hotArr)
  60. }) { (error) in
  61. }
  62. }
  63. func creatLabels(hotArr: [String]){
  64. let margin: CGFloat = 10
  65. var lastBtn = UIButton()
  66. lastBtn.right = -10
  67. for (index,value) in hotArr.enumerated() {
  68. if value.count > 0 {
  69. let width: CGFloat = self.widthForComment(fontSize: 12, height: 12, text: value)
  70. let btn = UIButton(frame: CGRect(x: lastBtn.right+margin, y: 26, width: width+20, height: 22))
  71. btn.setTitle(value, for: .normal)
  72. btn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
  73. btn.setTitleColor(UIColor.white, for: .normal)
  74. btn.layer.cornerRadius = 11
  75. btn.backgroundColor = UIColor.init(white: 1, alpha: 0.2)
  76. btn.addTarget(self, action: #selector(clickAction(button:)), for: .touchUpInside)
  77. btn.tag = 1000+index
  78. lastBtn = btn
  79. self.scrollView?.addSubview(btn)
  80. }
  81. self.scrollView?.contentSize = CGSize(width: lastBtn.right+10, height: 74)
  82. }
  83. }
  84. func widthForComment(fontSize: CGFloat, height: CGFloat = 15,text: String) -> CGFloat {
  85. let font = UIFont.systemFont(ofSize: fontSize)
  86. let rect = text.boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: height), options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: font], context: nil)
  87. return ceil(rect.width)
  88. }
  89. @objc func clickAction(button: UIButton) {
  90. let index = button.tag - 1000
  91. let text = self.hotArr[index]
  92. self.clickClosure?(text)
  93. }
  94. @objc func clickSearchAction() {
  95. self.clickSearch?()
  96. }
  97. }