PQEditPublicTitleView.swift 11 KB

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