SwipeActionButton.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 intrinsicContentSize: CGSize {
  29. return CGSize(width: UIView.noIntrinsicMetric, height: contentEdgeInsets.top + alignmentRect.height + contentEdgeInsets.bottom)
  30. }
  31. convenience init(action: SwipeAction) {
  32. self.init(frame: .zero)
  33. contentHorizontalAlignment = .center
  34. tintColor = action.textColor ?? .white
  35. let highlightedTextColor = action.highlightedTextColor ?? tintColor
  36. highlightedBackgroundColor = action.highlightedBackgroundColor ?? UIColor.black.withAlphaComponent(0.1)
  37. titleLabel?.font = action.font ?? UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.medium)
  38. titleLabel?.textAlignment = .center
  39. titleLabel?.lineBreakMode = .byWordWrapping
  40. titleLabel?.numberOfLines = 0
  41. accessibilityLabel = action.accessibilityLabel
  42. setTitle(action.title, for: .normal)
  43. setTitleColor(tintColor, for: .normal)
  44. setTitleColor(highlightedTextColor, for: .highlighted)
  45. setImage(action.image, for: .normal)
  46. setImage(action.highlightedImage ?? action.image, for: .highlighted)
  47. }
  48. override var isHighlighted: Bool {
  49. didSet {
  50. guard shouldHighlight else { return }
  51. backgroundColor = isHighlighted ? highlightedBackgroundColor : .clear
  52. }
  53. }
  54. func preferredWidth(maximum: CGFloat) -> CGFloat {
  55. let width = maximum > 0 ? maximum : CGFloat.greatestFiniteMagnitude
  56. let textWidth = titleBoundingRect(with: CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)).width
  57. let imageWidth = currentImage?.size.width ?? 0
  58. return min(width, max(textWidth, imageWidth) + contentEdgeInsets.left + contentEdgeInsets.right)
  59. }
  60. func titleBoundingRect(with size: CGSize) -> CGRect {
  61. guard let title = currentTitle, let font = titleLabel?.font else { return .zero }
  62. return title.boundingRect(with: size,
  63. options: [.usesLineFragmentOrigin],
  64. attributes: [NSAttributedString.Key.font: font],
  65. context: nil).integral
  66. }
  67. override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
  68. var rect = contentRect.center(size: titleBoundingRect(with: contentRect.size).size)
  69. rect.origin.y = alignmentRect.minY + imageHeight + currentSpacing
  70. return rect.integral
  71. }
  72. override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
  73. var rect = contentRect.center(size: currentImage?.size ?? .zero)
  74. rect.origin.y = alignmentRect.minY + (imageHeight - rect.height) / 2
  75. return rect
  76. }
  77. }
  78. extension CGRect {
  79. func center(size: CGSize) -> CGRect {
  80. let dx = width - size.width
  81. let dy = height - size.height
  82. return CGRect(x: origin.x + dx * 0.5, y: origin.y + dy * 0.5, width: size.width, height: size.height)
  83. }
  84. }