分类:
.h中
typedef NS_ENUM(NSUInteger, HJButtonEdgeInsetsStyle) {
HJButtonEdgeInsetsStyleTop, // image在上,label在下
HJButtonEdgeInsetsStyleLeft, // image在左,label在右
HJButtonEdgeInsetsStyleBottom, // image在下,label在上
HJButtonEdgeInsetsStyleRight // image在右,label在左
}; - (void)layoutButtonWithEdgeInsetsStyle:(HJButtonEdgeInsetsStyle)style imageTitleSpace:(CGFloat)space;
--
space 是图片与文字的间距。
.m中
- (void)layoutButtonWithEdgeInsetsStyle:(HJButtonEdgeInsetsStyle)style imageTitleSpace:(CGFloat)space
{
switch (style) {
case HJButtonEdgeInsetsStyleTop:
{
[self setImageEdgeInsets:UIEdgeInsetsMake(-self.titleLabel.intrinsicContentSize.height, 0, 0, -self.titleLabel.intrinsicContentSize.width)];
[self setTitleEdgeInsets:UIEdgeInsetsMake(self.currentImage.size.height+space, -self.currentImage.size.width, 0, 0)];
}
break;
case HJButtonEdgeInsetsStyleLeft:
{
[self setTitleEdgeInsets:UIEdgeInsetsMake(0, space, 0, 0)];
}
break;
case HJButtonEdgeInsetsStyleBottom:
{
[self setImageEdgeInsets:UIEdgeInsetsMake(self.titleLabel.intrinsicContentSize.height, 0, 0, -self.titleLabel.intrinsicContentSize.width)];
[self setTitleEdgeInsets:UIEdgeInsetsMake(-self.currentImage.size.height-space, -self.currentImage.size.width, 0, 0)];
}
break;
case HJButtonEdgeInsetsStyleRight:
{
[self setImageEdgeInsets:UIEdgeInsetsMake(0,self.titleLabel.intrinsicContentSize.width , 0, -self.titleLabel.intrinsicContentSize.width)];
[self setTitleEdgeInsets:UIEdgeInsetsMake(0, -self.currentImage.size.width-space, 0, self.currentImage.size.width+space)];
}
break;
default:
break;
}
}
或者自定义,如果达不到需求(图片与文字宽度大于按钮宽度会压缩)。