SwipeButton.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // SwipeButton.m
  3. // SwipeTableView
  4. //
  5. // Created by zhao on 16/8/11.
  6. // Copyright © 2016年 zhaoName. All rights reserved.
  7. //
  8. #import "SwipeButton.h"
  9. #define NULL_STRING(string) [string isEqualToString:@""] || !string
  10. @implementation SwipeButton
  11. //只有title
  12. + (SwipeButton *)createSwipeButtonWithTitle:(NSString *)title backgroundColor:(UIColor *)backgroundColor touchBlock:(TouchSwipeButtonBlock)block
  13. {
  14. return [self createSwipeButtonWithTitle:title font:15 textColor:[UIColor blackColor] backgroundColor:backgroundColor touchBlock:block];
  15. }
  16. + (SwipeButton *)createSwipeButtonWithTitle:(NSString *)title font:(CGFloat)font textColor:(UIColor *)textColor backgroundColor:(UIColor *)backgroundColor touchBlock:(TouchSwipeButtonBlock)block
  17. {
  18. return [self createSwipeButtonWithTitle:title font:font textColor:textColor backgroundColor:backgroundColor image:nil touchBlock:block];
  19. }
  20. //只有图片
  21. + (SwipeButton *)createSwipeButtonWithImage:(UIImage *)image backgroundColor:(UIColor *)color touchBlock:(TouchSwipeButtonBlock)block
  22. {
  23. return [self createSwipeButtonWithTitle:nil font:15 textColor:[UIColor blackColor] backgroundColor:color image:image touchBlock:block];
  24. }
  25. //图片、文字都有,且图片在上 文字在下
  26. + (SwipeButton *)createSwipeButtonWithTitle:(NSString *)title backgroundColor:(UIColor *)backgroundColor image:(UIImage *)image touchBlock:(TouchSwipeButtonBlock)block
  27. {
  28. return [self createSwipeButtonWithTitle:title font:15 textColor:[UIColor blackColor] backgroundColor:backgroundColor image:image touchBlock:block];
  29. }
  30. + (SwipeButton *)createSwipeButtonWithTitle:(NSString *)title font:(CGFloat)font textColor:(UIColor *)textColor backgroundColor:(UIColor *)backgroundColor image:(UIImage *)image touchBlock:(TouchSwipeButtonBlock)block
  31. {
  32. SwipeButton *button = [self buttonWithType:UIButtonTypeCustom];
  33. [button setTitle:title forState:UIControlStateNormal];
  34. button.titleLabel.font = [UIFont systemFontOfSize:font];
  35. [button setTitleColor:textColor forState:UIControlStateNormal];
  36. button.backgroundColor = backgroundColor;
  37. [button setImage:image forState:UIControlStateNormal];
  38. button.touchBlock = block;
  39. // 算出文字的size
  40. CGSize titleSize = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, button.titleLabel.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil].size;
  41. // button的宽度去文字和图片两个中的最大宽度 其它值将在SwipeView中设置
  42. button.frame = CGRectMake(0, 0, MAX(titleSize.width+10, image.size.width+10), 0);
  43. if(!NULL_STRING(title) && !image)
  44. {
  45. button.titleEdgeInsets = UIEdgeInsetsMake(image.size.height, -image.size.width, 0, 0);
  46. button.imageEdgeInsets = UIEdgeInsetsMake(-titleSize.height, 0.5*titleSize.width, 0.5*titleSize.height, 0);
  47. }
  48. return button;
  49. }
  50. /**
  51. * 防止文字太长或图片太大 导致图片或文字的位置不在中间
  52. */
  53. - (void)layoutSubviews
  54. {
  55. [super layoutSubviews];
  56. if(self.titleLabel.text && self.imageView.image)
  57. {
  58. CGFloat marginH = (self.frame.size.height - self.imageView.frame.size.height - self.titleLabel.frame.size.height)/3;
  59. //图片
  60. CGPoint imageCenter = self.imageView.center;
  61. imageCenter.x = self.frame.size.width/2;
  62. imageCenter.y = self.imageView.frame.size.height/2 + marginH;
  63. self.imageView.center = imageCenter;
  64. //文字
  65. CGRect newFrame = self.titleLabel.frame;
  66. newFrame.origin.x = 0;
  67. newFrame.origin.y = self.frame.size.height - newFrame.size.height - marginH;
  68. newFrame.size.width = self.frame.size.width;
  69. self.titleLabel.frame = newFrame;
  70. self.titleLabel.textAlignment = NSTextAlignmentCenter;
  71. }
  72. }
  73. @end