123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- /*
- * MGSwipeTableCell is licensed under MIT license. See LICENSE.md file for more information.
- * Copyright (c) 2016 Imanol Fernandez @MortimerGoro
- */
- #import "MGSwipeButton.h"
- @class MGSwipeTableCell;
- @implementation MGSwipeButton
- +(instancetype) buttonWithTitle:(NSString *) title backgroundColor:(UIColor *) color
- {
- return [self buttonWithTitle:title icon:nil backgroundColor:color];
- }
- +(instancetype) buttonWithTitle:(NSString *) title backgroundColor:(UIColor *) color padding:(NSInteger) padding
- {
- return [self buttonWithTitle:title icon:nil backgroundColor:color insets:UIEdgeInsetsMake(0, padding, 0, padding)];
- }
- +(instancetype) buttonWithTitle:(NSString *) title backgroundColor:(UIColor *) color insets:(UIEdgeInsets) insets
- {
- return [self buttonWithTitle:title icon:nil backgroundColor:color insets:insets];
- }
- +(instancetype) buttonWithTitle:(NSString *) title backgroundColor:(UIColor *) color callback:(MGSwipeButtonCallback) callback
- {
- return [self buttonWithTitle:title icon:nil backgroundColor:color callback:callback];
- }
- +(instancetype) buttonWithTitle:(NSString *) title backgroundColor:(UIColor *) color padding:(NSInteger) padding callback:(MGSwipeButtonCallback) callback
- {
- return [self buttonWithTitle:title icon:nil backgroundColor:color insets:UIEdgeInsetsMake(0, padding, 0, padding) callback:callback];
- }
- +(instancetype) buttonWithTitle:(NSString *) title backgroundColor:(UIColor *) color insets:(UIEdgeInsets) insets callback:(MGSwipeButtonCallback) callback
- {
- return [self buttonWithTitle:title icon:nil backgroundColor:color insets:insets callback:callback];
- }
- +(instancetype) buttonWithTitle:(NSString *) title icon:(UIImage*) icon backgroundColor:(UIColor *) color
- {
- return [self buttonWithTitle:title icon:icon backgroundColor:color callback:nil];
- }
- +(instancetype) buttonWithTitle:(NSString *) title icon:(UIImage*) icon backgroundColor:(UIColor *) color padding:(NSInteger) padding
- {
- return [self buttonWithTitle:title icon:icon backgroundColor:color insets:UIEdgeInsetsMake(0, padding, 0, padding) callback:nil];
- }
- +(instancetype) buttonWithTitle:(NSString *) title icon:(UIImage*) icon backgroundColor:(UIColor *) color insets:(UIEdgeInsets) insets
- {
- return [self buttonWithTitle:title icon:icon backgroundColor:color insets:insets callback:nil];
- }
- +(instancetype) buttonWithTitle:(NSString *) title icon:(UIImage*) icon backgroundColor:(UIColor *) color callback:(MGSwipeButtonCallback) callback
- {
- return [self buttonWithTitle:title icon:icon backgroundColor:color padding:10 callback:callback];
- }
- +(instancetype) buttonWithTitle:(NSString *) title icon:(UIImage*) icon backgroundColor:(UIColor *) color padding:(NSInteger) padding callback:(MGSwipeButtonCallback) callback
- {
- return [self buttonWithTitle:title icon:icon backgroundColor:color insets:UIEdgeInsetsMake(0, padding, 0, padding) callback:callback];
- }
- +(instancetype) buttonWithTitle:(NSString *) title icon:(UIImage*) icon backgroundColor:(UIColor *) color insets:(UIEdgeInsets) insets callback:(MGSwipeButtonCallback) callback
- {
- MGSwipeButton * button = [self buttonWithType:UIButtonTypeCustom];
- button.backgroundColor = color;
- button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
- button.titleLabel.textAlignment = NSTextAlignmentCenter;
- [button setTitle:title forState:UIControlStateNormal];
- [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
- [button setImage:icon forState:UIControlStateNormal];
- button.callback = callback;
- [button setEdgeInsets:insets];
- return button;
- }
- -(BOOL) callMGSwipeConvenienceCallback: (MGSwipeTableCell *) sender
- {
- if (_callback) {
- return _callback(sender);
- }
- return NO;
- }
- -(void) centerIconOverText
- {
- [self centerIconOverTextWithSpacing: 3.0];
- }
- -(void) centerIconOverTextWithSpacing: (CGFloat) spacing {
- CGSize size = self.imageView.image.size;
-
- if ([UIDevice currentDevice].systemVersion.floatValue >= 9.0 && [UIApplication sharedApplication].userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft) {
- self.titleEdgeInsets = UIEdgeInsetsMake(0.0,
- 0.0,
- -(size.height + spacing),
- -size.width);
- size = [self.titleLabel.text sizeWithAttributes:@{ NSFontAttributeName: self.titleLabel.font }];
- self.imageEdgeInsets = UIEdgeInsetsMake(-(size.height + spacing),
- -size.width,
- 0.0,
- 0.0);
- }
- else
- {
- self.titleEdgeInsets = UIEdgeInsetsMake(0.0,
- -size.width,
- -(size.height + spacing),
- 0.0);
- size = [self.titleLabel.text sizeWithAttributes:@{ NSFontAttributeName: self.titleLabel.font }];
- self.imageEdgeInsets = UIEdgeInsetsMake(-(size.height + spacing),
- 0.0,
- 0.0,
- -size.width);
- }
- }
- -(void) setPadding:(CGFloat) padding
- {
- self.contentEdgeInsets = UIEdgeInsetsMake(0, padding, 0, padding);
- [self sizeToFit];
- }
- - (void)setButtonWidth:(CGFloat)buttonWidth
- {
- _buttonWidth = buttonWidth;
- if (_buttonWidth > 0)
- {
- CGRect frame = self.frame;
- frame.size.width = _buttonWidth;
- self.frame = frame;
- }
- else
- {
- [self sizeToFit];
- }
- }
- -(void) setEdgeInsets:(UIEdgeInsets)insets
- {
- self.contentEdgeInsets = insets;
- [self sizeToFit];
- }
- -(void) iconTintColor:(UIColor *)tintColor
- {
- UIImage *currentIcon = self.imageView.image;
- if (currentIcon.renderingMode != UIImageRenderingModeAlwaysTemplate) {
- currentIcon = [currentIcon imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
- [self setImage:currentIcon forState:UIControlStateNormal];
- }
- self.tintColor = tintColor;
- }
- @end
|