123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- //
- // YMSearchPageHeaderView.swift
- // MvpGoods
- //
- // Created by 小花 on 2019/4/12.
- // Copyright © 2019 MVP. All rights reserved.
- //
- import UIKit
- class YMSearchPageHeaderView: UIView {
- typealias ClickClosure = (_ text: String) -> Void
- typealias ClickSearch = () -> Void
- var clickClosure: ClickClosure?
- var clickSearch: ClickSearch?
- var scrollView: UIScrollView?
- var hotArr = [String]()
-
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- override init(frame: CGRect) {
- super.init(frame: frame)
- initSubViews()
- loadHotData()
- }
-
- func initSubViews() {
- let searchBg = UIView(frame: CGRect(x: 10, y: 0, width: self.width-20, height: 38))
- searchBg.backgroundColor = UIColor.white
- searchBg.layer.cornerRadius = 3
- self.addSubview(searchBg)
-
- let searchBtn = UIButton(frame: CGRect(x: 10, y: 0, width: searchBg.width, height: searchBg.height))
- searchBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
- searchBtn.setTitleColor(UIColor.hexadecimalColor(hexadecimal: "#666666"), for: .normal)
- searchBtn.set(image: UIImage(named: "search_gray"), title: "输入淘宝天猫商品标题或关键词", titlePosition: .right, additionalSpacing: 5, state: .normal)
- searchBg.addSubview(searchBtn)
- searchBtn.addTarget(self, action: #selector(clickSearchAction), for: .touchUpInside)
- searchBtn.contentHorizontalAlignment = .left
- searchBg.addSubview(searchBtn)
-
- let rightBtn = UIButton(frame: CGRect(x: searchBg.width-67, y: 63, width: 63, height: 34))
- rightBtn.centerY = searchBg.height/2
- rightBtn.setTitle("搜索", for: .normal)
- rightBtn.backgroundColor = UIColor.gloadRedColor()
- rightBtn.layer.cornerRadius = 3
- rightBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
- rightBtn.setTitleColor(UIColor.white, for: .normal)
- searchBg.addSubview(rightBtn)
-
- let scrollView = UIScrollView(frame: CGRect(x: 10, y: searchBg.bottom, width: self.width-20, height: 74))
- scrollView.showsHorizontalScrollIndicator = false
- self.addSubview(scrollView)
- self.scrollView = scrollView
- }
-
- func loadHotData() {
- let url = BASE_URL + "/api/v2/channel/hotsearch"
- NetWorkTool.getRequest(url: url, param: [:], success: { (json) in
- let items = json["rst"]?["data"].arrayObject
-
- for item in items ?? [] {
- let str = item as! [String: Any]
- let text = str["name"] as! String
- self.hotArr.append(text)
- }
- self.creatLabels(hotArr: self.hotArr)
-
- }) { (error) in
-
- }
- }
-
- func creatLabels(hotArr: [String]){
- let margin: CGFloat = 10
- var lastBtn = UIButton()
- lastBtn.right = -10
- for (index,value) in hotArr.enumerated() {
- if value.count > 0 {
- let width: CGFloat = self.widthForComment(fontSize: 12, height: 12, text: value)
- let btn = UIButton(frame: CGRect(x: lastBtn.right+margin, y: 26, width: width+20, height: 22))
- btn.setTitle(value, for: .normal)
- btn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
- btn.setTitleColor(UIColor.white, for: .normal)
- btn.layer.cornerRadius = 11
- btn.backgroundColor = UIColor.init(white: 1, alpha: 0.2)
- btn.addTarget(self, action: #selector(clickAction(button:)), for: .touchUpInside)
- btn.tag = 1000+index
- lastBtn = btn
- self.scrollView?.addSubview(btn)
- }
- self.scrollView?.contentSize = CGSize(width: lastBtn.right+10, height: 74)
- }
- }
-
-
- func widthForComment(fontSize: CGFloat, height: CGFloat = 15,text: String) -> CGFloat {
- let font = UIFont.systemFont(ofSize: fontSize)
- let rect = text.boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: height), options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: font], context: nil)
- return ceil(rect.width)
- }
-
- @objc func clickAction(button: UIButton) {
- let index = button.tag - 1000
- let text = self.hotArr[index]
- self.clickClosure?(text)
- }
-
- @objc func clickSearchAction() {
- self.clickSearch?()
- }
- }
|