MGSwipeButton.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * MGSwipeTableCell is licensed under MIT license. See LICENSE.md file for more information.
  3. * Copyright (c) 2016 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. {
  74. [self centerIconOverTextWithSpacing: 3.0];
  75. }
  76. -(void) centerIconOverTextWithSpacing: (CGFloat) spacing {
  77. CGSize size = self.imageView.image.size;
  78. if ([UIDevice currentDevice].systemVersion.floatValue >= 9.0 && [UIApplication sharedApplication].userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft) {
  79. self.titleEdgeInsets = UIEdgeInsetsMake(0.0,
  80. 0.0,
  81. -(size.height + spacing),
  82. -size.width);
  83. size = [self.titleLabel.text sizeWithAttributes:@{ NSFontAttributeName: self.titleLabel.font }];
  84. self.imageEdgeInsets = UIEdgeInsetsMake(-(size.height + spacing),
  85. -size.width,
  86. 0.0,
  87. 0.0);
  88. }
  89. else
  90. {
  91. self.titleEdgeInsets = UIEdgeInsetsMake(0.0,
  92. -size.width,
  93. -(size.height + spacing),
  94. 0.0);
  95. size = [self.titleLabel.text sizeWithAttributes:@{ NSFontAttributeName: self.titleLabel.font }];
  96. self.imageEdgeInsets = UIEdgeInsetsMake(-(size.height + spacing),
  97. 0.0,
  98. 0.0,
  99. -size.width);
  100. }
  101. }
  102. -(void) setPadding:(CGFloat) padding
  103. {
  104. self.contentEdgeInsets = UIEdgeInsetsMake(0, padding, 0, padding);
  105. [self sizeToFit];
  106. }
  107. - (void)setButtonWidth:(CGFloat)buttonWidth
  108. {
  109. _buttonWidth = buttonWidth;
  110. if (_buttonWidth > 0)
  111. {
  112. CGRect frame = self.frame;
  113. frame.size.width = _buttonWidth;
  114. self.frame = frame;
  115. }
  116. else
  117. {
  118. [self sizeToFit];
  119. }
  120. }
  121. -(void) setEdgeInsets:(UIEdgeInsets)insets
  122. {
  123. self.contentEdgeInsets = insets;
  124. [self sizeToFit];
  125. }
  126. -(void) iconTintColor:(UIColor *)tintColor
  127. {
  128. UIImage *currentIcon = self.imageView.image;
  129. if (currentIcon.renderingMode != UIImageRenderingModeAlwaysTemplate) {
  130. currentIcon = [currentIcon imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
  131. [self setImage:currentIcon forState:UIControlStateNormal];
  132. }
  133. self.tintColor = tintColor;
  134. }
  135. @end