PQEditPublicTitleView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //
  2. // PQEditPublicTitleView.swift
  3. // BFFramework
  4. //
  5. // Created by ak on 2021/7/22.
  6. // 功能:编辑标题
  7. import Foundation
  8. import BFUIKit
  9. class PQEditPublicTitleView: UIView {
  10. //点击确认回调
  11. public var confirmBtnClock: ((_ selectTitle: String?) -> Void)?
  12. //VIEW 隐藏回调事件
  13. public var viewIsHiddenCallBack: (() -> Void)?
  14. lazy var backView: UIView = {
  15. let backView = UIView()
  16. backView.addCorner(corner: 1.5)
  17. backView.backgroundColor = .white
  18. return backView
  19. }()
  20. lazy var closeView: UIView = {
  21. let closeView = UIView()
  22. closeView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.8)
  23. return closeView
  24. }()
  25. // 输入框
  26. lazy var inputTV: BFTextView = {
  27. let inputTV = BFTextView()
  28. inputTV.font = UIFont.systemFont(ofSize: 17, weight: .regular)
  29. inputTV.backgroundColor = .clear
  30. inputTV.textColor = .black
  31. inputTV.maxTextLength = 30
  32. inputTV.placeHolderDefultPoint = CGPoint(x: 5, y: 0)
  33. inputTV.tintColor = UIColor.hexColor(hexadecimal: BFConfig.shared.styleColor.rawValue)
  34. inputTV.placeHolder = "我见过你眼中的春与秋,胜过我见过的所有山川河流"
  35. inputTV.showsVerticalScrollIndicator = false
  36. inputTV.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  37. return inputTV
  38. }()
  39. // 输入框背景
  40. lazy var inputBgView: UIView = {
  41. let inputBgView = UIView()
  42. inputBgView.backgroundColor = .clear
  43. inputBgView.addCorner(corner: 10)
  44. inputBgView.layer.cornerRadius = 7
  45. inputBgView.layer.borderWidth = 2
  46. inputBgView.layer.borderColor = UIColor.hexColor(hexadecimal: BFConfig.shared.styleColor.rawValue).cgColor
  47. return inputBgView
  48. }()
  49. // 确定按钮
  50. lazy var confirmBtn: UIButton = {
  51. let confirmBtn = UIButton()
  52. confirmBtn.backgroundColor = UIColor.hexColor(hexadecimal: BFConfig.shared.styleColor.rawValue)
  53. confirmBtn.setTitle("确定", for: .normal)
  54. confirmBtn.setTitleColor(.white, for: .normal)
  55. confirmBtn.setTitleColor(UIColor.white, for: .selected)
  56. confirmBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: .medium)
  57. confirmBtn.addCorner(corner: 3)
  58. confirmBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
  59. return confirmBtn
  60. }()
  61. // 标题列表
  62. lazy var titleCollectionView: UICollectionView = {
  63. let flowLayout = UICollectionViewFlowLayout()
  64. flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  65. flowLayout.minimumLineSpacing = 0
  66. flowLayout.minimumInteritemSpacing = 0
  67. flowLayout.scrollDirection = .vertical
  68. let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
  69. collectionView.showsVerticalScrollIndicator = false
  70. collectionView.showsHorizontalScrollIndicator = false
  71. collectionView.delegate = self
  72. collectionView.dataSource = self
  73. collectionView.backgroundColor = .clear
  74. collectionView.register(PQEditPublicTitleViewContentCell.self, forCellWithReuseIdentifier: String(describing: PQEditPublicTitleViewContentCell.self))
  75. if #available(iOS 11.0, *) {
  76. collectionView.contentInsetAdjustmentBehavior = .never
  77. }
  78. // 延迟scrollView上子视图的响应,所以当直接拖动UISlider时,如果此时touch时间在150ms以内,UIScrollView会认为是拖动自己,从而拦截了event,导致UISlider接收不到滑动的event
  79. collectionView.delaysContentTouches = false
  80. return collectionView
  81. }()
  82. // 标题数据
  83. var titles: Array<String> = Array() {
  84. didSet {
  85. titleCollectionView.reloadData()
  86. }
  87. }
  88. override init(frame: CGRect) {
  89. super.init(frame: frame)
  90. let ges = UITapGestureRecognizer(target: self, action: #selector(viewClick))
  91. closeView.addGestureRecognizer(ges)
  92. backgroundColor = .clear
  93. addSubview(closeView)
  94. addSubview(backView)
  95. backView.addSubview(inputBgView)
  96. inputBgView.addSubview(inputTV)
  97. inputBgView.addSubview(confirmBtn)
  98. backView.addSubview(titleCollectionView)
  99. backView.snp.makeConstraints { make in
  100. make.left.width.bottom.equalToSuperview()
  101. make.height.equalTo(min(640, cScreenHeigth))
  102. // make.width.equalTo(cScreenWidth)
  103. // make.height.equalTo(min(640, cScreenHeigth))
  104. // make.bottom.equalToSuperview()
  105. }
  106. closeView.snp.makeConstraints { make in
  107. make.right.equalToSuperview()
  108. make.width.equalTo(cScreenWidth)
  109. make.height.equalTo(cScreenHeigth - 640)
  110. make.top.equalToSuperview()
  111. }
  112. inputBgView.snp.makeConstraints { make in
  113. make.left.equalToSuperview().offset(16)
  114. make.right.equalToSuperview().offset(-16)
  115. make.height.equalTo(68)
  116. make.top.equalToSuperview().offset(20)
  117. }
  118. inputTV.snp.makeConstraints { make in
  119. make.left.equalToSuperview().offset(14)
  120. make.width.equalTo(259)
  121. make.height.equalTo(48)
  122. make.top.equalToSuperview().offset(10)
  123. }
  124. confirmBtn.snp.makeConstraints { make in
  125. make.right.equalToSuperview()
  126. make.width.equalTo(58)
  127. make.height.equalTo(68)
  128. make.top.equalToSuperview()
  129. }
  130. titleCollectionView.snp.makeConstraints { make in
  131. make.top.equalTo(inputBgView.snp.bottom).offset(10)
  132. make.width.right.equalToSuperview()
  133. make.bottom.equalTo(0 - cAKSafeAreaHeight)
  134. // make.width.equalTo(cScreenWidth)
  135. // make.height.equalTo(542 - cAKSafeAreaHeight)
  136. }
  137. // titleCollectionView.reloadData()
  138. }
  139. required init?(coder _: NSCoder) {
  140. fatalError("init(coder:) has not been implemented")
  141. }
  142. /// 按钮点击事件
  143. @objc func btnClick(sender _: UIButton?) {
  144. BFLog(message: "点击了确定 \(String(describing: inputTV.text))")
  145. if confirmBtnClock != nil {
  146. confirmBtnClock!(inputTV.text)
  147. inputTV.text = ""
  148. }
  149. viewClick()
  150. }
  151. @objc func viewClick() {
  152. self.isHidden = true
  153. inputTV.resignFirstResponder()
  154. if viewIsHiddenCallBack != nil{
  155. viewIsHiddenCallBack!()
  156. }
  157. }
  158. //显示界面
  159. func show() {
  160. isHidden = false
  161. inputTV.becomeFirstResponder()
  162. }
  163. }
  164. extension PQEditPublicTitleView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate {
  165. func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {
  166. return titles.count
  167. }
  168. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  169. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: PQEditPublicTitleViewContentCell.self), for: indexPath) as! PQEditPublicTitleViewContentCell
  170. cell.titleStr = titles[indexPath.row]
  171. return cell
  172. }
  173. func collectionView(_: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  174. BFLog(message: "选择了 \(titles[indexPath.item])")
  175. inputTV.text = titles[indexPath.item]
  176. }
  177. func collectionView(_ collectionView: UICollectionView, layout _: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  178. let title = titles[indexPath.row]
  179. let textSize = sizeWithText(text: title, font: UIFont.systemFont(ofSize: 17, weight: .regular), size: CGSize.init(width: 295, height: CGFloat.greatestFiniteMagnitude))
  180. //28 是 cell label 上下边距总和
  181. return CGSize(width: cScreenWidth, height: textSize.height + 28)
  182. }
  183. func scrollViewWillBeginDecelerating(_: UIScrollView) {
  184. inputTV.resignFirstResponder()
  185. }
  186. }
  187. class PQEditPublicTitleViewContentCell: UICollectionViewCell {
  188. lazy var titleLab: UILabel = {
  189. let titleLab = UILabel()
  190. titleLab.font = UIFont.systemFont(ofSize: 17, weight: .regular)
  191. titleLab.textColor = .black
  192. titleLab.numberOfLines = 0
  193. titleLab.lineBreakMode = .byCharWrapping
  194. titleLab.isUserInteractionEnabled = true
  195. titleLab.textAlignment = .left
  196. return titleLab
  197. }()
  198. // 手势提示
  199. lazy var iconView: UIImageView = {
  200. let iconView = UIImageView()
  201. iconView.backgroundColor = .clear
  202. iconView.image = UIImage.moduleImage(named: "editTitleTips", moduleName: "BFFramework",isAssets: false)
  203. return iconView
  204. }()
  205. lazy var lineView: UIView = {
  206. let lineView = UIView()
  207. lineView.backgroundColor = UIColor.hexColor(hexadecimal: "#EFEFEF")
  208. return lineView
  209. }()
  210. override init(frame: CGRect) {
  211. super.init(frame: frame)
  212. contentView.addSubview(titleLab)
  213. contentView.addSubview(iconView)
  214. contentView.addSubview(lineView)
  215. addLayout()
  216. }
  217. required init?(coder _: NSCoder) {
  218. fatalError("init(coder:) has not been implemented")
  219. }
  220. var titleStr: String? {
  221. didSet {
  222. titleLab.text = titleStr
  223. if(titleLab.text?.count ?? 0 == 0){
  224. lineView.backgroundColor = .white
  225. iconView.isHidden = true
  226. }else{
  227. lineView.backgroundColor = UIColor.hexColor(hexadecimal: "#EFEFEF")
  228. iconView.isHidden = false
  229. }
  230. addLayout()
  231. }
  232. }
  233. func addLayout() {
  234. let textSize = sizeWithText(text: titleStr ?? "", font: UIFont.systemFont(ofSize: 17, weight: .regular), size: CGSize.init(width: 295, height: CGFloat.greatestFiniteMagnitude))
  235. titleLab.snp.remakeConstraints { make in
  236. make.height.equalTo(textSize.height * cAdaptatWidth)
  237. make.right.equalToSuperview().offset(-64)
  238. make.left.equalToSuperview().offset(16)
  239. make.top.equalToSuperview().offset(12)
  240. }
  241. lineView.snp.makeConstraints { make in
  242. make.right.equalToSuperview().offset(-16)
  243. make.left.equalToSuperview().offset(16)
  244. make.bottom.equalToSuperview().offset(-1)
  245. make.height.equalTo(1)
  246. }
  247. iconView.snp.remakeConstraints { make in
  248. make.width.height.equalTo(24)
  249. make.right.equalToSuperview().offset(-16)
  250. make.top.equalTo(contentView.snp.top).offset(14)
  251. }
  252. }
  253. }