CXSpacingButton.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // CXSpacingButton.swift
  3. // TitleImageEdgeinsetsButton
  4. //
  5. // Created by chunxi on 2019/1/10.
  6. // Copyright © 2019 chunxi. All rights reserved.
  7. //
  8. import UIKit
  9. /*
  10. 默认的图片在左文字在右侧
  11. */
  12. public enum ImagePosition {
  13. case left
  14. case right
  15. case bottom
  16. case top
  17. }
  18. public class CXSpacingButton: UIButton {
  19. public var spacing: CGFloat = 0
  20. public var imagePosition: ImagePosition = .left
  21. override public func layoutSubviews() {
  22. super.layoutSubviews()
  23. layoutImagePosition()
  24. }
  25. private func layoutImagePosition() {
  26. let content = super.contentRect(forBounds: bounds)
  27. let titleRect = super.titleRect(forContentRect: content)
  28. let imageRect = super.imageRect(forContentRect: content)
  29. print(content,titleRect,imageRect)
  30. switch imagePosition {
  31. case .left:
  32. switch contentHorizontalAlignment {
  33. case .center:
  34. titleEdgeInsets.updateLeftAndRight(left: spacing / 2)
  35. imageEdgeInsets.updateLeftAndRight(left: -spacing / 2)
  36. case .left:
  37. titleEdgeInsets.updateLeftAndRight(left: spacing)
  38. imageEdgeInsets.updateLeftAndRight(left: 0)
  39. case .right:
  40. titleEdgeInsets.updateLeftAndRight(left: 0)
  41. imageEdgeInsets.updateLeftAndRight(left: -spacing)
  42. default:
  43. break
  44. }
  45. case .right:
  46. var imageOffset = titleRect.width
  47. var titleOffset = imageRect.width
  48. switch contentHorizontalAlignment {
  49. case .center:
  50. imageOffset = imageOffset + spacing/2
  51. titleOffset = titleOffset + spacing/2
  52. case .left:
  53. imageOffset = imageOffset + spacing
  54. case .right:
  55. titleOffset = titleOffset + spacing
  56. default:
  57. break
  58. }
  59. imageEdgeInsets.updateLeftAndRight(left: imageOffset)
  60. titleEdgeInsets.updateLeftAndRight(left: -titleOffset)
  61. case .top:
  62. let allWidth = imageRect.width + titleRect.width
  63. let allHeight = imageRect.height + titleRect.height
  64. let maxHeight = max(imageRect.height, titleRect.height)
  65. switch contentHorizontalAlignment {
  66. case .center:
  67. let imageOffsetX = allWidth/2 - imageRect.width/2
  68. let titleOffsetX = allWidth/2 - titleRect.width/2
  69. imageEdgeInsets.updateLeftAndRight(left: imageOffsetX)
  70. titleEdgeInsets.updateLeftAndRight(left: -titleOffsetX)
  71. case .left:
  72. titleEdgeInsets.updateLeftAndRight(left: -imageRect.width)
  73. imageEdgeInsets.updateLeftAndRight(left: 0)
  74. case .right:
  75. titleEdgeInsets.updateLeftAndRight(left: 0)
  76. imageEdgeInsets.updateLeftAndRight(left: titleRect.width)
  77. default:
  78. break
  79. }
  80. switch contentVerticalAlignment {
  81. case .center:
  82. let baseHeight = allHeight - maxHeight
  83. let imageOffsetY = baseHeight/2 + spacing/2
  84. let titleOffsetY = baseHeight/2 + spacing/2
  85. imageEdgeInsets.updateTopAndBottom(top: -imageOffsetY)
  86. titleEdgeInsets.updateTopAndBottom(top: titleOffsetY)
  87. case .top:
  88. imageEdgeInsets.updateTopAndBottom(top: 0)
  89. titleEdgeInsets.updateTopAndBottom(top: imageRect.height + spacing)
  90. case .bottom:
  91. imageEdgeInsets.updateTopAndBottom(top: -titleRect.height - spacing)
  92. titleEdgeInsets.updateTopAndBottom(top: 0)
  93. default:
  94. break
  95. }
  96. case .bottom:
  97. let allWidth = imageRect.width + titleRect.width
  98. let allHeight = imageRect.height + titleRect.height
  99. let maxHeight = max(imageRect.height, titleRect.height)
  100. switch contentHorizontalAlignment {
  101. case .center:
  102. let imageOffsetX = allWidth/2 - imageRect.width/2
  103. let titleOffsetX = allWidth/2 - titleRect.width/2
  104. imageEdgeInsets.updateLeftAndRight(left: imageOffsetX)
  105. titleEdgeInsets.updateLeftAndRight(left: -titleOffsetX)
  106. case .left:
  107. titleEdgeInsets.updateLeftAndRight(left: -imageRect.width)
  108. imageEdgeInsets.updateLeftAndRight(left: 0)
  109. case .right:
  110. titleEdgeInsets.updateLeftAndRight(left: 0)
  111. imageEdgeInsets.updateLeftAndRight(left: titleRect.width)
  112. default:
  113. break
  114. }
  115. switch contentVerticalAlignment {
  116. case .center:
  117. let baseHeight = allHeight - maxHeight
  118. let imageOffsetY = baseHeight/2 + spacing/2
  119. let titleOffsetY = baseHeight/2 + spacing/2
  120. imageEdgeInsets.updateTopAndBottom(top: imageOffsetY)
  121. titleEdgeInsets.updateTopAndBottom(top: -titleOffsetY)
  122. case .top:
  123. imageEdgeInsets.updateTopAndBottom(top: titleRect.height + spacing)
  124. titleEdgeInsets.updateTopAndBottom(top: 0)
  125. case .bottom:
  126. imageEdgeInsets.updateTopAndBottom(top: 0)
  127. titleEdgeInsets.updateTopAndBottom(top: -imageRect.height - spacing)
  128. default:
  129. break
  130. }
  131. }
  132. }
  133. }