UIButton+Extension.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // UIButton+Extension.swift
  3. // AiKeyboard
  4. //
  5. // Created by Destiny on 2025/4/23.
  6. //
  7. import Foundation
  8. import UIKit
  9. // MARK: - UIButton 图片 与 title 位置关系
  10. extension UIButton {
  11. /// 图片 和 title 的布局样式
  12. enum ImageTitleLayout {
  13. case imgTop
  14. case imgBottom
  15. case imgLeft
  16. case imgRight
  17. }
  18. // MARK: 3.1、设置图片和 title 的位置关系(提示:title和image要在设置布局关系之前设置)
  19. /// 设置图片和 title 的位置关系(提示:title和image要在设置布局关系之前设置)
  20. /// - Parameters:
  21. /// - layout: 布局
  22. /// - spacing: 间距
  23. /// - Returns: 返回自身
  24. @discardableResult
  25. func setImageTitleLayout(_ layout: ImageTitleLayout, spacing: CGFloat = 0) -> UIButton {
  26. switch layout {
  27. case .imgLeft:
  28. alignHorizontal(spacing: spacing, imageFirst: true)
  29. case .imgRight:
  30. alignHorizontal(spacing: spacing, imageFirst: false)
  31. case .imgTop:
  32. alignVertical(spacing: spacing, imageTop: true)
  33. case .imgBottom:
  34. alignVertical(spacing: spacing, imageTop: false)
  35. }
  36. return self
  37. }
  38. /// 水平方向
  39. /// - Parameters:
  40. /// - spacing: 间距
  41. /// - imageFirst: 图片是否优先
  42. private func alignHorizontal(spacing: CGFloat, imageFirst: Bool) {
  43. let edgeOffset = spacing / 2
  44. imageEdgeInsets = UIEdgeInsets(top: 0, left: -edgeOffset,
  45. bottom: 0,right: edgeOffset)
  46. titleEdgeInsets = UIEdgeInsets(top: 0, left: edgeOffset,
  47. bottom: 0, right: -edgeOffset)
  48. if !imageFirst {
  49. transform = CGAffineTransform(scaleX: -1, y: 1)
  50. imageView?.transform = CGAffineTransform(scaleX: -1, y: 1)
  51. titleLabel?.transform = CGAffineTransform(scaleX: -1, y: 1)
  52. }
  53. contentEdgeInsets = UIEdgeInsets(top: 0, left: edgeOffset, bottom: 0, right: edgeOffset)
  54. }
  55. /// 垂直方向
  56. /// - Parameters:
  57. /// - spacing: 间距
  58. /// - imageTop: 图片是不是在顶部
  59. private func alignVertical(spacing: CGFloat, imageTop: Bool) {
  60. guard let imageWidth = self.imageView?.kb_width,
  61. let imageHeight = self.imageView?.kb_height,
  62. let text = self.titleLabel?.text,
  63. let font = self.titleLabel?.font
  64. else {
  65. return
  66. }
  67. let labelString = NSString(string: text)
  68. let titleSize = labelString.size(withAttributes: [NSAttributedString.Key.font: font])
  69. let titleHeight = titleSize.height
  70. let titleWidth = titleSize.width
  71. let insetAmount = spacing / 2
  72. if imageTop {
  73. imageEdgeInsets = UIEdgeInsets(top: -titleHeight - insetAmount,
  74. left: (self.kb_width - imageWidth) / 2,
  75. bottom: 0,
  76. right: (self.kb_width - imageWidth) / 2 - titleWidth)
  77. titleEdgeInsets = UIEdgeInsets(top: 0,
  78. left: -imageWidth,
  79. bottom: -imageHeight - insetAmount,
  80. right: 0)
  81. } else {
  82. imageEdgeInsets = UIEdgeInsets(top: 0,
  83. left: (self.kb_width - imageWidth) / 2,
  84. bottom: -titleHeight - insetAmount,
  85. right: (self.kb_width - imageWidth) / 2 - titleWidth)
  86. titleEdgeInsets = UIEdgeInsets(top: -imageHeight - insetAmount,
  87. left: -imageWidth,
  88. bottom: 0,
  89. right: 0)
  90. }
  91. }
  92. }