天天省钱快报

UIButton+WSLTitleImage.m 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // UIButton+WSLTitleImage.m
  3. // collectionView
  4. //
  5. // Created by 王双龙 on 2017/11/6.
  6. // Copyright © 2017年 王双龙. All rights reserved.
  7. //
  8. #import "UIButton+WSLTitleImage.h"
  9. @implementation UIButton (WSLTitleImage)
  10. - (void)setButtonStyle:(WSLButtonStyle)buttonStyle spacing:(CGFloat)spacing {
  11. [self setTitle:self.currentTitle forState:UIControlStateNormal];
  12. [self setImage:self.currentImage forState:UIControlStateNormal];
  13. CGFloat imageWidth = self.imageView.image.size.width;
  14. CGFloat imageHeight = self.imageView.image.size.height;
  15. // CGFloat labelWidth = [self.titleLabel.text sizeWithFont:self.titleLabel.font].width;
  16. // CGFloat labelHeight = [self.titleLabel.text sizeWithFont:self.titleLabel.font].height;
  17. CGFloat labelWidth = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}].width;
  18. CGFloat labelHeight = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}].height;
  19. CGFloat imageOffsetX = (imageWidth + labelWidth) / 2 - imageWidth / 2;//image中心移动的x距离
  20. CGFloat imageOffsetY = imageHeight / 2 + spacing / 2;//image中心移动的y距离
  21. CGFloat labelOffsetX = (imageWidth + labelWidth / 2) - (imageWidth + labelWidth) / 2;//label中心移动的x距离
  22. CGFloat labelOffsetY = labelHeight / 2 + spacing / 2;//label中心移动的y距离
  23. CGFloat tempWidth = MAX(labelWidth, imageWidth);
  24. CGFloat changedWidth = labelWidth + imageWidth - tempWidth;
  25. CGFloat tempHeight = MAX(labelHeight, imageHeight);
  26. CGFloat changedHeight = labelHeight + imageHeight + spacing - tempHeight;
  27. switch (buttonStyle) {
  28. case WSLButtonStyleImageLeft:
  29. self.imageEdgeInsets = UIEdgeInsetsMake(0, -spacing/2, 0, spacing/2);
  30. self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, -spacing/2);
  31. self.contentEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2);
  32. break;
  33. case WSLButtonStyleImageRight:
  34. self.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth + spacing/2, 0, -(labelWidth + spacing/2));
  35. self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageWidth + spacing/2), 0, imageWidth + spacing/2);
  36. self.contentEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2);
  37. break;
  38. case WSLButtonStyleImageTop:
  39. self.imageEdgeInsets = UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX);
  40. self.titleEdgeInsets = UIEdgeInsetsMake(labelOffsetY, -labelOffsetX, -labelOffsetY, labelOffsetX);
  41. self.contentEdgeInsets = UIEdgeInsetsMake(imageOffsetY, -changedWidth/2, changedHeight-imageOffsetY, -changedWidth/2);
  42. break;
  43. case WSLButtonStyleImageBottom:
  44. self.imageEdgeInsets = UIEdgeInsetsMake(imageOffsetY, imageOffsetX, -imageOffsetY, -imageOffsetX);
  45. self.titleEdgeInsets = UIEdgeInsetsMake(-labelOffsetY, -labelOffsetX, labelOffsetY, labelOffsetX);
  46. self.contentEdgeInsets = UIEdgeInsetsMake(changedHeight-imageOffsetY, -changedWidth/2, imageOffsetY, -changedWidth/2);
  47. break;
  48. default:
  49. break;
  50. }
  51. }
  52. @end