// // UIButton+WSLTitleImage.m // collectionView // // Created by 王双龙 on 2017/11/6. // Copyright © 2017年 王双龙. All rights reserved. // #import "UIButton+WSLTitleImage.h" @implementation UIButton (WSLTitleImage) - (void)setButtonStyle:(WSLButtonStyle)buttonStyle spacing:(CGFloat)spacing { [self setTitle:self.currentTitle forState:UIControlStateNormal]; [self setImage:self.currentImage forState:UIControlStateNormal]; CGFloat imageWidth = self.imageView.image.size.width; CGFloat imageHeight = self.imageView.image.size.height; // CGFloat labelWidth = [self.titleLabel.text sizeWithFont:self.titleLabel.font].width; // CGFloat labelHeight = [self.titleLabel.text sizeWithFont:self.titleLabel.font].height; CGFloat labelWidth = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}].width; CGFloat labelHeight = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}].height; CGFloat imageOffsetX = (imageWidth + labelWidth) / 2 - imageWidth / 2;//image中心移动的x距离 CGFloat imageOffsetY = imageHeight / 2 + spacing / 2;//image中心移动的y距离 CGFloat labelOffsetX = (imageWidth + labelWidth / 2) - (imageWidth + labelWidth) / 2;//label中心移动的x距离 CGFloat labelOffsetY = labelHeight / 2 + spacing / 2;//label中心移动的y距离 CGFloat tempWidth = MAX(labelWidth, imageWidth); CGFloat changedWidth = labelWidth + imageWidth - tempWidth; CGFloat tempHeight = MAX(labelHeight, imageHeight); CGFloat changedHeight = labelHeight + imageHeight + spacing - tempHeight; switch (buttonStyle) { case WSLButtonStyleImageLeft: self.imageEdgeInsets = UIEdgeInsetsMake(0, -spacing/2, 0, spacing/2); self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, -spacing/2); self.contentEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2); break; case WSLButtonStyleImageRight: self.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth + spacing/2, 0, -(labelWidth + spacing/2)); self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageWidth + spacing/2), 0, imageWidth + spacing/2); self.contentEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2); break; case WSLButtonStyleImageTop: self.imageEdgeInsets = UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX); self.titleEdgeInsets = UIEdgeInsetsMake(labelOffsetY, -labelOffsetX, -labelOffsetY, labelOffsetX); self.contentEdgeInsets = UIEdgeInsetsMake(imageOffsetY, -changedWidth/2, changedHeight-imageOffsetY, -changedWidth/2); break; case WSLButtonStyleImageBottom: self.imageEdgeInsets = UIEdgeInsetsMake(imageOffsetY, imageOffsetX, -imageOffsetY, -imageOffsetX); self.titleEdgeInsets = UIEdgeInsetsMake(-labelOffsetY, -labelOffsetX, labelOffsetY, labelOffsetX); self.contentEdgeInsets = UIEdgeInsetsMake(changedHeight-imageOffsetY, -changedWidth/2, imageOffsetY, -changedWidth/2); break; default: break; } } @end