UIButton+Layout.m 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. //
  2. // UIButton+Layout.m
  3. // LenzCameraNativeModuleForRN
  4. //
  5. // Created by 王昭威 on 2023/1/23.
  6. //
  7. #import "UIButton+Layout.h"
  8. @implementation UIButton (Layout)
  9. - (void)centerVerticallyWithPadding:(float)padding {
  10. CGSize imageSize = self.imageView.frame.size;
  11. CGSize titleSize = self.titleLabel.frame.size;
  12. CGFloat totalHeight = (imageSize.height + titleSize.height + padding);
  13. self.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height),
  14. 0.0f,
  15. 0.0f,
  16. - titleSize.width);
  17. self.titleEdgeInsets = UIEdgeInsetsMake(0.0f,
  18. - imageSize.width,
  19. - (totalHeight - titleSize.height),
  20. 0.0f);
  21. self.contentEdgeInsets = UIEdgeInsetsMake(0.0f,
  22. 0.0f,
  23. titleSize.height,
  24. 0.0f);
  25. }
  26. - (void)centerVertically {
  27. const CGFloat kDefaultPadding = 6.0f;
  28. [self centerVerticallyWithPadding:kDefaultPadding];
  29. }
  30. @end