MGSwipeButton.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * MGSwipeTableCell is licensed under MIT license. See LICENSE.md file for more information.
  3. * Copyright (c) 2014 Imanol Fernandez @MortimerGoro
  4. */
  5. #import "MGSwipeButton.h"
  6. @class MGSwipeTableCell;
  7. @implementation MGSwipeButton
  8. +(instancetype) buttonWithTitle:(NSString *) title backgroundColor:(UIColor *) color
  9. {
  10. return [self buttonWithTitle:title icon:nil backgroundColor:color];
  11. }
  12. +(instancetype) buttonWithTitle:(NSString *) title backgroundColor:(UIColor *) color padding:(NSInteger) padding
  13. {
  14. return [self buttonWithTitle:title icon:nil backgroundColor:color insets:UIEdgeInsetsMake(0, padding, 0, padding)];
  15. }
  16. +(instancetype) buttonWithTitle:(NSString *) title backgroundColor:(UIColor *) color insets:(UIEdgeInsets) insets
  17. {
  18. return [self buttonWithTitle:title icon:nil backgroundColor:color insets:insets];
  19. }
  20. +(instancetype) buttonWithTitle:(NSString *) title backgroundColor:(UIColor *) color callback:(MGSwipeButtonCallback) callback
  21. {
  22. return [self buttonWithTitle:title icon:nil backgroundColor:color callback:callback];
  23. }
  24. +(instancetype) buttonWithTitle:(NSString *) title backgroundColor:(UIColor *) color padding:(NSInteger) padding callback:(MGSwipeButtonCallback) callback
  25. {
  26. return [self buttonWithTitle:title icon:nil backgroundColor:color insets:UIEdgeInsetsMake(0, padding, 0, padding) callback:callback];
  27. }
  28. +(instancetype) buttonWithTitle:(NSString *) title backgroundColor:(UIColor *) color insets:(UIEdgeInsets) insets callback:(MGSwipeButtonCallback) callback
  29. {
  30. return [self buttonWithTitle:title icon:nil backgroundColor:color insets:insets callback:callback];
  31. }
  32. +(instancetype) buttonWithTitle:(NSString *) title icon:(UIImage*) icon backgroundColor:(UIColor *) color
  33. {
  34. return [self buttonWithTitle:title icon:icon backgroundColor:color callback:nil];
  35. }
  36. +(instancetype) buttonWithTitle:(NSString *) title icon:(UIImage*) icon backgroundColor:(UIColor *) color padding:(NSInteger) padding
  37. {
  38. return [self buttonWithTitle:title icon:icon backgroundColor:color insets:UIEdgeInsetsMake(0, padding, 0, padding) callback:nil];
  39. }
  40. +(instancetype) buttonWithTitle:(NSString *) title icon:(UIImage*) icon backgroundColor:(UIColor *) color insets:(UIEdgeInsets) insets
  41. {
  42. return [self buttonWithTitle:title icon:icon backgroundColor:color insets:insets callback:nil];
  43. }
  44. +(instancetype) buttonWithTitle:(NSString *) title icon:(UIImage*) icon backgroundColor:(UIColor *) color callback:(MGSwipeButtonCallback) callback
  45. {
  46. return [self buttonWithTitle:title icon:icon backgroundColor:color padding:10 callback:callback];
  47. }
  48. +(instancetype) buttonWithTitle:(NSString *) title icon:(UIImage*) icon backgroundColor:(UIColor *) color padding:(NSInteger) padding callback:(MGSwipeButtonCallback) callback
  49. {
  50. return [self buttonWithTitle:title icon:icon backgroundColor:color insets:UIEdgeInsetsMake(0, padding, 0, padding) callback:callback];
  51. }
  52. +(instancetype) buttonWithTitle:(NSString *) title icon:(UIImage*) icon backgroundColor:(UIColor *) color insets:(UIEdgeInsets) insets callback:(MGSwipeButtonCallback) callback
  53. {
  54. MGSwipeButton * button = [self buttonWithType:UIButtonTypeCustom];
  55. button.backgroundColor = color;
  56. button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
  57. button.titleLabel.textAlignment = NSTextAlignmentCenter;
  58. [button setTitle:title forState:UIControlStateNormal];
  59. [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  60. [button setImage:icon forState:UIControlStateNormal];
  61. button.callback = callback;
  62. [button setEdgeInsets:insets];
  63. return button;
  64. }
  65. -(BOOL) callMGSwipeConvenienceCallback: (MGSwipeTableCell *) sender
  66. {
  67. if (_callback) {
  68. return _callback(sender);
  69. }
  70. return NO;
  71. }
  72. -(void) centerIconOverText {
  73. const CGFloat spacing = 3.0;
  74. CGSize size = self.imageView.image.size;
  75. self.titleEdgeInsets = UIEdgeInsetsMake(0.0,
  76. -size.width,
  77. -(size.height + spacing),
  78. 0.0);
  79. size = [self.titleLabel.text sizeWithAttributes:@{ NSFontAttributeName: self.titleLabel.font }];
  80. self.imageEdgeInsets = UIEdgeInsetsMake(-(size.height + spacing),
  81. 0.0,
  82. 0.0,
  83. -size.width);
  84. }
  85. -(void) setPadding:(CGFloat) padding
  86. {
  87. self.contentEdgeInsets = UIEdgeInsetsMake(0, padding, 0, padding);
  88. [self sizeToFit];
  89. }
  90. - (void)setButtonWidth:(CGFloat)buttonWidth
  91. {
  92. _buttonWidth = buttonWidth;
  93. if (_buttonWidth > 0)
  94. {
  95. CGRect frame = self.frame;
  96. frame.size.width = _buttonWidth;
  97. self.frame = frame;
  98. }
  99. else
  100. {
  101. [self sizeToFit];
  102. }
  103. }
  104. -(void) setEdgeInsets:(UIEdgeInsets)insets
  105. {
  106. self.contentEdgeInsets = insets;
  107. [self sizeToFit];
  108. }
  109. -(void)layoutSubviews
  110. {
  111. [super layoutSubviews];
  112. // Center image
  113. // [self setWidth:self.frame.size.width+20];
  114. CGPoint center = self.imageView.center;
  115. center.x = self.frame.size.width/2;
  116. center.y = self.imageView.frame.size.height/2+10;
  117. self.imageView.center = center;
  118. if (!self.imageView.image) {
  119. //self.titleLabel.frame=self.frame;
  120. }
  121. else {
  122. //Center text
  123. CGRect newFrame = [self titleLabel].frame;
  124. newFrame.origin.x = 0;
  125. newFrame.origin.y = self.imageView.frame.size.height + 5;
  126. newFrame.size.width = self.frame.size.width;
  127. self.titleLabel.frame = newFrame;
  128. }
  129. self.titleLabel.textAlignment = NSTextAlignmentCenter;
  130. }
  131. @end