1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- //
- // UIButton+YMImageLocation.m
- // MSYOUPAI
- //
- // Created by YoMi on 2024/3/9.
- // Copyright © 2024 MS. All rights reserved.
- //
- #import "UIButton+YMImageLocation.h"
- @implementation UIButton (YMImageLocation)
- - (void)ym_imageLocationStyle:(YMImageLocationStyle)imageLocationStyle spacing:(CGFloat)spacing {
- if (imageLocationStyle == YMImageLocationStyleLeft) {
- if (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentLeft) {
- self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing, 0, 0);
- } else if (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentRight) {
- self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, spacing);
- } else {
- self.imageEdgeInsets = UIEdgeInsetsMake(0, - 0.5 * spacing, 0, 0.5 * spacing);
- self.titleEdgeInsets = UIEdgeInsetsMake(0, 0.5 * spacing, 0, - 0.5 * spacing);
- }
- } else if (imageLocationStyle == YMImageLocationStyleRight) {
- CGFloat imageW = self.imageView.image.size.width;
- CGFloat titleW = self.titleLabel.frame.size.width;
- if (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentLeft) {
- self.imageEdgeInsets = UIEdgeInsetsMake(0, titleW + spacing, 0, 0);
- self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageW, 0, 0);
- } else if (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentRight) {
- self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, - titleW);
- self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, imageW + spacing);
- } else {
- CGFloat imageOffset = titleW + 0.5 * spacing;
- CGFloat titleOffset = imageW + 0.5 * spacing;
- self.imageEdgeInsets = UIEdgeInsetsMake(0, imageOffset, 0, - imageOffset);
- self.titleEdgeInsets = UIEdgeInsetsMake(0, - titleOffset, 0, titleOffset);
- }
- } else if (imageLocationStyle == YMImageLocationStyleTop) {
- CGFloat imageW = self.imageView.frame.size.width;
- CGFloat imageH = self.imageView.frame.size.height;
- CGFloat titleIntrinsicContentSizeW = self.titleLabel.intrinsicContentSize.width;
- CGFloat titleIntrinsicContentSizeH = self.titleLabel.intrinsicContentSize.height;
- self.imageEdgeInsets = UIEdgeInsetsMake(- titleIntrinsicContentSizeH - spacing, 0, 0, - titleIntrinsicContentSizeW);
- self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageW, - imageH - spacing, 0);
- } else if (imageLocationStyle == YMImageLocationStyleBottom) {
- CGFloat imageW = self.imageView.frame.size.width;
- CGFloat imageH = self.imageView.frame.size.height;
- CGFloat titleIntrinsicContentSizeW = self.titleLabel.intrinsicContentSize.width;
- CGFloat titleIntrinsicContentSizeH = self.titleLabel.intrinsicContentSize.height;
- self.imageEdgeInsets = UIEdgeInsetsMake(titleIntrinsicContentSizeH + spacing, 0, 0, - titleIntrinsicContentSizeW);
- self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageW, imageH + spacing, 0);
- }
- }
- - (void)ym_imageLocationStyle:(YMImageLocationStyle)imageLocationStyle spacing:(CGFloat)spacing imageLocationBlock:(void (^)(UIButton *button))imageLocationBlock {
-
- imageLocationBlock(self);
-
- [self ym_imageLocationStyle:imageLocationStyle spacing:spacing];
- }
- @end
|