PQEditPublicTitleView.swift 10 KB

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