KeyboardExchangeCell.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // KeyboardExchangeCell.swift
  3. // AiKeyboard
  4. //
  5. // Created by Destiny on 2025/4/28.
  6. //
  7. import UIKit
  8. import Kingfisher
  9. class KeyboardExchangeCell: UICollectionViewCell {
  10. static func reuseIdentifier() -> String {
  11. return "KeyboardExchangeCell"
  12. }
  13. var keyboardModel: KeyboardModel?
  14. lazy var containerView: UIView = {
  15. let view = UIView()
  16. view.layer.cornerRadius = 14
  17. view.backgroundColor = .white
  18. return view
  19. }()
  20. lazy var iconImageView: UIImageView = {
  21. let imageView = UIImageView()
  22. imageView.image = UIImage(named: "keyboard_exchange_normal")
  23. imageView.clipsToBounds = true
  24. imageView.layer.cornerRadius = 8
  25. return imageView
  26. }()
  27. lazy var keyboardLabel: UILabel = {
  28. let label = UILabel()
  29. label.text = "通用键盘"
  30. label.font = .systemFont(ofSize: 15, weight: .medium)
  31. label.textColor = .hexStringColor(hexString: "#000000", alpha: 0.8)
  32. return label
  33. }()
  34. override init(frame: CGRect) {
  35. super.init(frame: frame)
  36. initUI()
  37. }
  38. required init?(coder: NSCoder) {
  39. fatalError("init(coder:) has not been implemented")
  40. }
  41. func config(model: KeyboardModel) {
  42. self.keyboardModel = model
  43. self.keyboardLabel.text = model.name
  44. if model.type == "custom" {
  45. if let url = URL(string: model.imageUrl ?? "") {
  46. self.iconImageView.kf.setImage(with: url, placeholder: UIImage(named: "keyboard_exchange_normal"))
  47. }
  48. } else if model.type == "system" {
  49. self.iconImageView.image = UIImage(named: "keyboard_exchange_normal")
  50. }
  51. if model.isChoose == true {
  52. if let image = UIImage.gradient([UIColor.hexStringColor(hexString: "#7D46FC"), UIColor.hexStringColor(hexString: "#BC87FF")], size: CGSize(width: KeyboardExchangeView.UX.cellWidth, height: KeyboardExchangeView.UX.cellHeight), locations: [0, 1], direction: .horizontal) {
  53. containerView.backgroundColor = UIColor(patternImage: image)
  54. }
  55. self.keyboardLabel.textColor = .white
  56. } else {
  57. containerView.backgroundColor = .white
  58. self.keyboardLabel.textColor = .hexStringColor(hexString: "#000000", alpha: 0.8)
  59. }
  60. }
  61. }
  62. extension KeyboardExchangeCell {
  63. func initUI() {
  64. self.contentView.addSubview(containerView)
  65. containerView.snp.makeConstraints { make in
  66. make.edges.equalToSuperview()
  67. }
  68. containerView.addSubview(iconImageView)
  69. iconImageView.snp.makeConstraints { make in
  70. make.size.equalTo(CGSize(width: 36, height: 36))
  71. make.top.equalTo(10)
  72. make.centerX.equalToSuperview()
  73. }
  74. containerView.addSubview(keyboardLabel)
  75. keyboardLabel.snp.makeConstraints { make in
  76. make.top.equalTo(iconImageView.snp.bottom).offset(8)
  77. make.centerX.equalToSuperview()
  78. }
  79. }
  80. }