经常会碰到图片和字的位置不一样的按钮,记录在此,直接贴代码
.h 文件
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger,SAImageTitleLocationType) {
SAImageLocateLeftAndTotalCenter = 0,
SAImageLocateRightAndTotalCenter,
SAImageLocateLeftAndTotalLeft,
SAImageLocateRightAndTotalLeft,
SAImageLocateLeftAndTotalRight,
SAImageLocateRightAndTotalRight,
SAImageLocateTopAndTotalCenter,
SAImageLocateBottomAndTotalCenter,
};
@interface SAInsetButton : UIButton
/** image和title布局样式, 默认图左字右 */
@property (nonatomic, assign) SAImageTitleLocationType locationType;
/** 图片与文字的间距, 默认10 */
@property (nonatomic, assign) CGFloat interval;
@end
.m 文件
#import "SAInsetButton.h"
#define kDefaultInterval 10
@implementation SAInsetButton
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.interval = kDefaultInterval;
self.locationType = SAImageLocateLeftAndTotalCenter;
}
return self;
}
- (void)setLocationType:(SAImageTitleLocationType)locationType {
_locationType = locationType;
[self setNeedsLayout];
}
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat titleWidth = self.titleLabel.bounds.size.width;
CGFloat titleheight = self.titleLabel.bounds.size.height;
CGFloat imageWidth = self.currentImage.size.width;
CGFloat imageHeight = self.currentImage.size.height;
switch (self.locationType) {
case SAImageLocateLeftAndTotalLeft:
{
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
self.titleEdgeInsets = UIEdgeInsetsMake(0, _interval, 0,-_interval);
}
break;
case SAImageLocateLeftAndTotalCenter:
{
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
self.imageEdgeInsets = UIEdgeInsetsMake(0, -_interval*0.5, 0, _interval*0.5);
self.titleEdgeInsets = UIEdgeInsetsMake(0, _interval*0.5, 0,-_interval*0.5);
}
break;
case SAImageLocateLeftAndTotalRight:
{
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
self.imageEdgeInsets = UIEdgeInsetsMake(0, -_interval, 0, _interval);
self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0,0);
}
break;
case SAImageLocateRightAndTotalLeft:
{
self.imageEdgeInsets = UIEdgeInsetsMake(0,titleWidth+_interval, 0, -(titleWidth+_interval));
self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, 0, imageWidth);
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
}
break;
case SAImageLocateRightAndTotalCenter:
{
self.imageEdgeInsets = UIEdgeInsetsMake(0,titleWidth+_interval*0.5, 0, -(titleWidth+_interval*0.5));
self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageWidth+_interval*0.5), 0, imageWidth+_interval*0.5);
}
break;
case SAImageLocateRightAndTotalRight:
{
self.imageEdgeInsets = UIEdgeInsetsMake(0,titleWidth, 0, -(titleWidth));
self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageWidth+_interval), 0, imageWidth+_interval);
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
}
break;
case SAImageLocateTopAndTotalCenter:
{
self.imageEdgeInsets = UIEdgeInsetsMake(0,0, titleheight + _interval, -(titleWidth));
self.titleEdgeInsets = UIEdgeInsetsMake(imageHeight + _interval, -(imageWidth), 0, 0);
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
}
break;
case SAImageLocateBottomAndTotalCenter:
{
self.imageEdgeInsets = UIEdgeInsetsMake(0,0, -(titleheight + _interval), -titleWidth);
self.titleEdgeInsets = UIEdgeInsetsMake(-(imageHeight + _interval),-imageWidth, 0, 0);
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
}
break;
default:
break;
}
}
@end
部分代码来源于:帅炯(http://www.jianshu.com/u/5aacba215548)