封装一个button上带图片的,图片在上,文字在下的按钮

时间:2022-05-21 06:08:53

#import "CJShoppingDetailButton.h"

@implementation CJShoppingDetailButton

- (void)layoutSubviews{

[super layoutSubviews];

CGSize imageSize = self.imageView.image.size;

CGSize titleLabelSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font maxSize:CGSizeMake(self.width, self.height)];

CGFloat imageViewY = (self.height - imageSize.height - titleLabelSize.height) / 3.f;

CGFloat imageViewX = (self.width - imageSize.width) / 2.f;

self.imageView.frame = CGRectMake(imageViewX, imageViewY, imageSize.width, imageSize.height);

CGFloat titleLabelX = (self.width - titleLabelSize.width) / 2.f;

self.titleLabel.frame = CGRectMake(titleLabelX, self.imageView.bottom + imageViewY, titleLabelSize.width, titleLabelSize.height);

}

@end

#import <UIKit/UIKit.h>

@interface CJShoppingDetailButton : UIButton

@end