CustomImageTitleButton.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import UIKit
  2. enum imageTitleType: Int {
  3. case topImageBottomTitle = 0 //图片在上文字在下
  4. case topTitleBottomImage //文字在上图片在下
  5. case leftTitleRightImage //文字在左图片在右
  6. case leftImageRightTitle //图片在左文字在右(默认情况)
  7. }
  8. class CustomImageTitleButton: UIButton {
  9. /// 文字图片显示位置类型
  10. private var type: imageTitleType = .leftImageRightTitle
  11. /// 文字图片间距
  12. private var space: CGFloat = 0.0
  13. override func awakeFromNib() {
  14. super.awakeFromNib()
  15. self.contentHorizontalAlignment = .center
  16. self.contentVerticalAlignment = .center
  17. assert(self.imageView != nil, "没设置self.imageView")
  18. }
  19. override init(frame: CGRect) {
  20. super.init(frame: frame)
  21. awakeFromNib()
  22. }
  23. required init?(coder aDecoder: NSCoder) {
  24. super.init(coder: aDecoder)
  25. }
  26. override func updateConstraints() {
  27. resetEdgeInsets(type: type, space: space)
  28. super.updateConstraints()
  29. }
  30. /// 重置edgeinsets
  31. ///
  32. /// - Parameters:
  33. /// - type: 图片文字位置类型, 默认左图片右文字
  34. /// - space: 图片文字间距, 默认间距为0
  35. func resetEdgeInsets(type: imageTitleType = .leftImageRightTitle, space: CGFloat = 0.0) {
  36. self.type = type
  37. self.space = space
  38. let imageWidth = self.imageView!.frame.width
  39. let imageHeight = self.imageView!.frame.height
  40. var labelWidth = self.titleLabel!.frame.width
  41. var labelHeight = self.titleLabel!.frame.height
  42. // assert((imageWidth + labelWidth) > self.frame.width && (imageHeight + labelHeight) > self.frame.height, "button frame过小")
  43. if #available(iOS 8.0, *) {
  44. //高于 iOS 8.0
  45. labelWidth = self.titleLabel!.intrinsicContentSize.width
  46. labelHeight = self.titleLabel!.intrinsicContentSize.height
  47. }
  48. var titleTop, titleLeft, titleBottom, titleRight: CGFloat
  49. var imageTop, imageLeft, imageBottom, imageRight: CGFloat
  50. switch type {
  51. case .leftImageRightTitle:
  52. imageTop = 0
  53. imageBottom = 0
  54. imageLeft = -space / 2.0
  55. imageRight = -imageLeft
  56. titleTop = 0
  57. titleBottom = 0
  58. titleLeft = space / 2.0
  59. titleRight = titleLeft
  60. case .leftTitleRightImage:
  61. imageTop = 0
  62. imageBottom = 0
  63. imageLeft = labelWidth + space / 2.0
  64. imageRight = -imageLeft
  65. titleTop = 0
  66. titleBottom = 0
  67. titleLeft = -(imageWidth + space / 2.0)
  68. titleRight = -titleLeft
  69. case .topTitleBottomImage:
  70. imageTop = labelHeight / 2.0 + space / 2.0
  71. imageBottom = -imageTop
  72. imageLeft = (imageWidth + labelWidth) / 2.0 - imageWidth / 2.0
  73. imageRight = -imageLeft
  74. titleTop = -(imageHeight / 2.0 + space / 2.0)
  75. titleBottom = -titleTop
  76. //titleLeft偏移量 = imageWidth + labelWidth / 2.0 - (imageWidth + labelWidth) / 2.0, 计算之后为 imageWidth / 2.0
  77. titleLeft = -imageWidth / 2.0
  78. titleRight = -titleLeft
  79. case .topImageBottomTitle:
  80. imageTop = -(labelHeight / 2.0 + space / 2.0)
  81. imageBottom = -imageTop
  82. imageLeft = (imageWidth + labelWidth) / 2.0 - imageWidth / 2.0
  83. imageRight = -imageLeft
  84. titleTop = imageHeight / 2.0 + space / 2.0
  85. titleBottom = -titleTop
  86. //titleLeft偏移量 = imageWidth + labelWidth / 2.0 - (imageWidth + labelWidth) / 2.0, 计算之后为 imageWidth / 2.0
  87. titleLeft = -imageWidth / 2.0
  88. titleRight = -titleLeft
  89. }
  90. self.imageEdgeInsets = UIEdgeInsets(top: imageTop, left: imageLeft, bottom: imageBottom, right: imageRight)
  91. self.titleEdgeInsets = UIEdgeInsets(top: titleTop, left: titleLeft, bottom: titleBottom, right: titleRight)
  92. }
  93. }