// // XF_Segment.swift // xingchuangke // // Created by uen on 2020/6/28. // Copyright © 2020 Virgil. All rights reserved. // import UIKit public typealias ChooseItemClosure = (_ item: Int) -> Void class XF_Segment: UIView { private var chooseItemClosure: ChooseItemClosure? let lineHeight: CGFloat = 2 let fontSize: UIFont = UIFont.systemFont(ofSize: 14) var defaultColor: UIColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x333333, alpha: 1.0) var selectedColor: UIColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x2094f5, alpha: 1.0) var selectedIndex = 0 lazy var lineView: UIView = { let lineView = UIView.init() lineView.backgroundColor = self.selectedColor lineView.layer.cornerRadius = 1 return lineView }() lazy var scrollView: UIScrollView = { let scrollView = UIScrollView.init(frame: self.bounds) scrollView.backgroundColor = UIColor.clear scrollView.showsVerticalScrollIndicator = false scrollView.showsHorizontalScrollIndicator = false return scrollView }() var titles: [String] required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } public init(frame: CGRect, titles: [String], selectedIndex: NSInteger, defaultColor: UIColor, selectedColor: UIColor, chooseItemClosure:@escaping ChooseItemClosure) { self.titles = titles self.defaultColor = defaultColor self.selectedColor = selectedColor self.chooseItemClosure = chooseItemClosure self.selectedIndex = selectedIndex super.init(frame: frame) setup() } func setup() { self.addSubview(scrollView) let containerView = UIView.init(frame: self.bounds) containerView.backgroundColor = UIColor.clear scrollView.addSubview(containerView) var allBtnWidth: CGFloat = 0 for i in 0 ..< self.titles.count { let btn = UIButton.init(type: .custom) btn.tag = 100 + i btn.setTitle(titles[i], for: .normal) btn.setTitleColor(self.defaultColor, for: .normal) btn.setTitleColor(self.selectedColor, for: .selected) btn.titleLabel?.font = self.fontSize containerView.addSubview(btn) btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside) if i == self.selectedIndex { btn.isSelected = true } let textSize = titles[i].sizeWithText(font: self.fontSize, size: CGSize.init(width: 3000, height: 50)) btn.frame = CGRect.init(x: allBtnWidth, y: 0, width: textSize.width + 30, height: self.bounds.size.height) allBtnWidth += (textSize.width + 30) } // let textSize = titles[0].sizeWithText(font: self.fontSize, size: CGSize.init(width: 3000, height: 50)) var tits = "" for i in 0 ..< self.selectedIndex { tits += self.titles[i] } var textSize = tits.sizeWithText(font: self.fontSize, size: CGSize.init(width: 3000, height: 50)) let x: CGFloat = textSize.width + CGFloat(self.selectedIndex) * 30.0 textSize = titles[self.selectedIndex].sizeWithText(font: self.fontSize, size: CGSize.init(width: 3000, height: 50)) lineView.frame = CGRect.init(x: x + 15, y: self.bounds.size.height - 2, width: textSize.width, height: 2) containerView.addSubview(lineView) let width = (allBtnWidth > self.bounds.size.width) ? allBtnWidth : self.bounds.size.width containerView.setSizeWidth(width: width) scrollView.contentSize = CGSize.init(width: width, height: self.bounds.size.height) } @objc func btnClick(sender: UIButton) { for view in sender.superview!.subviews { if view.isKind(of: UIButton.self) { let btn: UIButton = view as! UIButton if btn.tag == sender.tag { btn.isSelected = true } else { btn.isSelected = false } } } let index = sender.tag - 100 var tits = "" for i in 0 ..< index { tits += self.titles[i] } var textSize = tits.sizeWithText(font: self.fontSize, size: CGSize.init(width: 3000, height: 50)) let x: CGFloat = textSize.width + CGFloat(index) * 30.0 textSize = titles[index].sizeWithText(font: self.fontSize, size: CGSize.init(width: 3000, height: 50)) let width: CGFloat = textSize.width UIView.animate(withDuration: 0.5) { self.lineView.frame = CGRect.init(x: x + 15, y: self.bounds.size.height - 2, width: width, height: 2) if x + width + 30 > ScreenWidth { self.scrollView.contentOffset.x = x + width + 30 - ScreenWidth } else if index == 0 && self.scrollView.contentOffset.x > 0 { self.scrollView.contentOffset.x = 0 } } chooseItemClosure!(sender.tag - 100) } /* // Only override draw() if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func draw(_ rect: CGRect) { // Drawing code } */ } extension String { // func sizeWithText(font: UIFont, size: CGSize) -> CGSize { //// let attributes = [] // // // } func sizeWithText(font: UIFont, size: CGSize) -> CGSize { let attributes = [NSAttributedString.Key.font: font] let option = NSStringDrawingOptions.usesLineFragmentOrigin let rect: CGRect = self.boundingRect(with: size, options: option, attributes: attributes, context: nil) return rect.size } }