SwipeActionButton.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // SwipeActionButton.swift
  3. //
  4. // Created by Jeremy Koch.
  5. // Copyright © 2017 Jeremy Koch. All rights reserved.
  6. //
  7. import UIKit
  8. class SwipeActionButton: UIButton {
  9. var spacing: CGFloat = 8
  10. var shouldHighlight = true
  11. var highlightedBackgroundColor: UIColor?
  12. var maximumImageHeight: CGFloat = 0
  13. var verticalAlignment: SwipeVerticalAlignment = .centerFirstBaseline
  14. var currentSpacing: CGFloat {
  15. return (currentTitle?.isEmpty == false && imageHeight > 0) ? spacing : 0
  16. }
  17. var alignmentRect: CGRect {
  18. let contentRect = self.contentRect(forBounds: bounds)
  19. let titleHeight = titleBoundingRect(with: verticalAlignment == .centerFirstBaseline ? CGRect.infinite.size : contentRect.size).integral.height
  20. let totalHeight = imageHeight + titleHeight + currentSpacing
  21. return contentRect.center(size: CGSize(width: contentRect.width, height: totalHeight))
  22. }
  23. private var imageHeight: CGFloat {
  24. get {
  25. return currentImage == nil ? 0 : maximumImageHeight
  26. }
  27. }
  28. override var frame: CGRect{
  29. set(newFrame){
  30. var frame = newFrame
  31. frame.origin.x = frame.origin.x + 5 //递归
  32. frame.origin.y = frame.origin.y + 10
  33. frame.size.width -= 10
  34. frame.size.height -= 20
  35. super.frame = frame
  36. }
  37. get{
  38. return super.frame
  39. }
  40. }
  41. override var intrinsicContentSize: CGSize {
  42. return CGSize(width: UIView.noIntrinsicMetric, height: contentEdgeInsets.top + alignmentRect.height + contentEdgeInsets.bottom)
  43. }
  44. convenience init(action: SwipeAction) {
  45. self.init(frame: .zero)
  46. contentHorizontalAlignment = .center
  47. tintColor = action.textColor ?? .white
  48. let highlightedTextColor = action.highlightedTextColor ?? tintColor
  49. highlightedBackgroundColor = action.highlightedBackgroundColor ?? UIColor.black.withAlphaComponent(0.1)
  50. titleLabel?.font = action.font ?? UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.medium)
  51. titleLabel?.textAlignment = .center
  52. titleLabel?.lineBreakMode = .byWordWrapping
  53. titleLabel?.numberOfLines = 0
  54. accessibilityLabel = action.accessibilityLabel
  55. setTitle(action.title, for: .normal)
  56. setTitleColor(tintColor, for: .normal)
  57. setTitleColor(highlightedTextColor, for: .highlighted)
  58. setImage(action.image, for: .normal)
  59. setImage(action.highlightedImage ?? action.image, for: .highlighted)
  60. layer.cornerRadius = 5
  61. layer.masksToBounds = true
  62. }
  63. override var isHighlighted: Bool {
  64. didSet {
  65. guard shouldHighlight else { return }
  66. backgroundColor = isHighlighted ? highlightedBackgroundColor : .clear
  67. }
  68. }
  69. func preferredWidth(maximum: CGFloat) -> CGFloat {
  70. let width = maximum > 0 ? maximum : CGFloat.greatestFiniteMagnitude
  71. let textWidth = titleBoundingRect(with: CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)).width
  72. let imageWidth = currentImage?.size.width ?? 0
  73. return min(width, max(textWidth, imageWidth) + contentEdgeInsets.left + contentEdgeInsets.right)
  74. }
  75. func titleBoundingRect(with size: CGSize) -> CGRect {
  76. guard let title = currentTitle, let font = titleLabel?.font else { return .zero }
  77. return title.boundingRect(with: size,
  78. options: [.usesLineFragmentOrigin],
  79. attributes: [NSAttributedString.Key.font: font],
  80. context: nil).integral
  81. }
  82. override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
  83. var rect = contentRect.center(size: titleBoundingRect(with: contentRect.size).size)
  84. rect.origin.y = alignmentRect.minY + imageHeight + currentSpacing
  85. return rect.integral
  86. }
  87. override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
  88. var rect = contentRect.center(size: currentImage?.size ?? .zero)
  89. rect.origin.y = alignmentRect.minY + (imageHeight - rect.height) / 2
  90. return rect
  91. }
  92. }
  93. extension CGRect {
  94. func center(size: CGSize) -> CGRect {
  95. let dx = width - size.width
  96. let dy = height - size.height
  97. return CGRect(x: origin.x + dx * 0.5, y: origin.y + dy * 0.5, width: size.width, height: size.height)
  98. }
  99. }