UIButton+YMImageLocation.m 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //
  2. // UIButton+YMImageLocation.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/3/9.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "UIButton+YMImageLocation.h"
  9. @implementation UIButton (YMImageLocation)
  10. - (void)ym_imageLocationStyle:(YMImageLocationStyle)imageLocationStyle spacing:(CGFloat)spacing {
  11. if (imageLocationStyle == YMImageLocationStyleLeft) {
  12. if (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentLeft) {
  13. self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing, 0, 0);
  14. } else if (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentRight) {
  15. self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, spacing);
  16. } else {
  17. self.imageEdgeInsets = UIEdgeInsetsMake(0, - 0.5 * spacing, 0, 0.5 * spacing);
  18. self.titleEdgeInsets = UIEdgeInsetsMake(0, 0.5 * spacing, 0, - 0.5 * spacing);
  19. }
  20. } else if (imageLocationStyle == YMImageLocationStyleRight) {
  21. CGFloat imageW = self.imageView.image.size.width;
  22. CGFloat titleW = self.titleLabel.frame.size.width;
  23. if (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentLeft) {
  24. self.imageEdgeInsets = UIEdgeInsetsMake(0, titleW + spacing, 0, 0);
  25. self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageW, 0, 0);
  26. } else if (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentRight) {
  27. self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, - titleW);
  28. self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, imageW + spacing);
  29. } else {
  30. CGFloat imageOffset = titleW + 0.5 * spacing;
  31. CGFloat titleOffset = imageW + 0.5 * spacing;
  32. self.imageEdgeInsets = UIEdgeInsetsMake(0, imageOffset, 0, - imageOffset);
  33. self.titleEdgeInsets = UIEdgeInsetsMake(0, - titleOffset, 0, titleOffset);
  34. }
  35. } else if (imageLocationStyle == YMImageLocationStyleTop) {
  36. CGFloat imageW = self.imageView.frame.size.width;
  37. CGFloat imageH = self.imageView.frame.size.height;
  38. CGFloat titleIntrinsicContentSizeW = self.titleLabel.intrinsicContentSize.width;
  39. CGFloat titleIntrinsicContentSizeH = self.titleLabel.intrinsicContentSize.height;
  40. self.imageEdgeInsets = UIEdgeInsetsMake(- titleIntrinsicContentSizeH - spacing, 0, 0, - titleIntrinsicContentSizeW);
  41. self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageW, - imageH - spacing, 0);
  42. } else if (imageLocationStyle == YMImageLocationStyleBottom) {
  43. CGFloat imageW = self.imageView.frame.size.width;
  44. CGFloat imageH = self.imageView.frame.size.height;
  45. CGFloat titleIntrinsicContentSizeW = self.titleLabel.intrinsicContentSize.width;
  46. CGFloat titleIntrinsicContentSizeH = self.titleLabel.intrinsicContentSize.height;
  47. self.imageEdgeInsets = UIEdgeInsetsMake(titleIntrinsicContentSizeH + spacing, 0, 0, - titleIntrinsicContentSizeW);
  48. self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageW, imageH + spacing, 0);
  49. }
  50. }
  51. - (void)ym_imageLocationStyle:(YMImageLocationStyle)imageLocationStyle spacing:(CGFloat)spacing imageLocationBlock:(void (^)(UIButton *button))imageLocationBlock {
  52. imageLocationBlock(self);
  53. [self ym_imageLocationStyle:imageLocationStyle spacing:spacing];
  54. }
  55. @end