Skip to content

Instantly share code, notes, and snippets.

@bluemoon2014
Last active June 12, 2018 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluemoon2014/9f22d8ba168350af6180a25dffb8f8ca to your computer and use it in GitHub Desktop.
Save bluemoon2014/9f22d8ba168350af6180a25dffb8f8ca to your computer and use it in GitHub Desktop.
自定义图片布局按钮
//
// mycButton.h
// 按钮布局
//
// Created by hdfx on 7/6/2018.
// Copyright © 2018 hdfx. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSUInteger, mycButtonStyle){
mycButtonImageLeft = 0, //图片在左
mycButtonImageRight = 1, //图片在右
mycButtonImageTop = 2, //图片在上
mycButtonImageBottom = 3, //图片在下
};
@interface mycButton : UIButton
/**
图片的位置,上、下、左、右,默认是图片居左
*/
@property (nonatomic, assign) mycButtonStyle buttonStyle;
/**
文字与图片之间的间距,默认是0
*/
@property (nonatomic, assign) CGFloat padding;
/**
创建button
@param buttonType button的类型
@param space 图片距离button的边距,如果图片比较大的,此时有效果;
如果图片比较小,没有效果,默认居中;
@return button
*/
+ (id)buttonWithType:(UIButtonType)buttonType withSpace:(CGFloat)space;
@end
//
// mycButton.m
// 按钮布局
//
// Created by hdfx on 7/6/2018.
// Copyright © 2018 hdfx. All rights reserved.
//
#import "mycButton.h"
@interface mycButton ()
/**
图片距离上下的距离
*/
@property (nonatomic, assign) CGFloat space;
@end
@implementation mycButton
+ (id)buttonWithType:(UIButtonType)buttonType withSpace:(CGFloat)space {
mycButton *button = [super buttonWithType:buttonType];
button.space = space;
return button;
}
- (void)layoutSubviews {
[super layoutSubviews];
NSLog(@"layout subviews");
//文案的宽度
CGFloat labelWidth = self.titleLabel.frame.size.width;
//文案的高度
CGFloat labelHeight = self.titleLabel.frame.size.height;
//button的image
UIImage *image = self.imageView.image;
switch (self.buttonStyle) {
case mycButtonImageLeft: {
//设置后的image显示的高度
CGFloat imageHeight = self.frame.size.height - (2 * self.space);
//文案和图片居中显示时距离两边的距离
CGFloat edgeSpace = (self.frame.size.width - imageHeight - labelWidth - self.padding) / 2;
self.imageEdgeInsets = UIEdgeInsetsMake(self.space, edgeSpace, self.space, edgeSpace + labelWidth + self.padding);
self.titleEdgeInsets = UIEdgeInsetsMake(0, -image.size.width + imageHeight + self.padding, 0, 0);
}
break;
case mycButtonImageRight: {
//设置后的image显示的高度
CGFloat imageHeight = self.frame.size.height - (2 * self.space);
//文案和图片居中显示时距离两边的距离
CGFloat edgeSpace = (self.frame.size.width - imageHeight - labelWidth - self.padding) / 2;
self.imageEdgeInsets = UIEdgeInsetsMake(self.space, edgeSpace + labelWidth + self.padding, self.space, edgeSpace);
self.titleEdgeInsets = UIEdgeInsetsMake(0, -image.size.width - self.padding - imageHeight, 0, 0);
}
break;
case mycButtonImageTop: {
//设置后的image显示的高度
CGFloat imageHeight = self.frame.size.height - (2 * self.space) - labelHeight - self.padding;
if (imageHeight > image.size.height) {
imageHeight = image.size.height;
}
self.imageEdgeInsets = UIEdgeInsetsMake(self.space, (self.frame.size.width - imageHeight) / 2, self.space + labelHeight + self.padding, (self.frame.size.width - imageHeight) / 2);
self.titleEdgeInsets = UIEdgeInsetsMake(self.space + imageHeight + self.padding, -image.size.width, self.space, 0);
}
break;
case mycButtonImageBottom: {
//设置后的image显示的高度
CGFloat imageHeight = self.frame.size.height - (2 * self.space) - labelHeight - self.padding;
if (imageHeight > image.size.height) {
imageHeight = image.size.height;
}
self.imageEdgeInsets = UIEdgeInsetsMake(self.space + labelHeight + self.padding, (self.frame.size.width - imageHeight) / 2, self.space, (self.frame.size.width - imageHeight) / 2);
self.titleEdgeInsets = UIEdgeInsetsMake(self.space, -image.size.width, self.padding + imageHeight + self.space, 0);
}
break;
default:
break;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment