最近忙的好久沒寫點啥了,哪怕寫一寫沒什麼價值的東西,也能體現出我還活着,不要板shi了,寫過的東西寫了一遍又一遍,呆zhi;佈局
設置button的圖片與文字位置,用系統自帶的控制其實也能夠;atom
.hspa
typedef NS_ENUM(NSUInteger, KLXKLayoutButtonStyle) { ///圖左字右 KLXKLayoutButtonStyleLeftImageRightTitle, ///圖右字左 KLXKLayoutButtonStyleLeftTitleRightImage, ///圖上字下 KLXKLayoutButtonStyleUpImageDownTitle, ///圖下字上 KLXKLayoutButtonStyleUpTitleDownImage }; @interface KLXKLayoutButton : UIButton /// 佈局方式 @property (nonatomic, assign) KLXKLayoutButtonStyle layoutStyle; /// 圖片和文字的間距,默認值 5 @property (nonatomic, assign) CGFloat imageTitleSpacing; /// 圖片 size 指定 @property (nonatomic, assign) CGSize imageSize; + (KLXKLayoutButton *)buttonWithLayoutButtonStyle:(KLXKLayoutButtonStyle)style title:(NSString *)title image:(UIImage *)image;
.mcode
#import "KLXKLayoutButton.h" @implementation KLXKLayoutButton + (KLXKLayoutButton *)buttonWithLayoutButtonStyle:(KLXKLayoutButtonStyle)style title:(NSString *)title image:(UIImage *)image{ KLXKLayoutButton *button = [KLXKLayoutButton buttonWithType:UIButtonTypeCustom]; button.adjustsImageWhenHighlighted = NO; [button setImage:image forState:UIControlStateNormal]; [button setTitle:title forState:UIControlStateNormal]; [button setTitleColor:[UIColor kl_colorBlack2] forState:UIControlStateNormal]; button.backgroundColor = [UIColor whiteColor]; button.layoutStyle = style; return button; } /// 初始化可用 /// @param frame KLXKLayoutButton - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.imageTitleSpacing = 5; self.imageSize = CGSizeZero; } return self; } /// 初始化可用 - (instancetype)init{ self = [super init]; if (self) { self.imageTitleSpacing = 5; self.imageSize = CGSizeZero; } return self; } - (void)layoutSubviews { [super layoutSubviews]; if (CGSizeEqualToSize(CGSizeZero, self.imageSize)) { [self.imageView sizeToFit]; } else { self.imageView.frame = CGRectMake(self.imageView.frame.origin.x, self.imageView.frame.origin.y, self.imageSize.width, self.imageSize.height); } [self.titleLabel sizeToFit]; switch (self.layoutStyle) { case KLXKLayoutButtonStyleLeftImageRightTitle: [self layoutHorizontalWithLeftView:self.imageView rightView:self.titleLabel]; break; case KLXKLayoutButtonStyleLeftTitleRightImage: [self layoutHorizontalWithLeftView:self.titleLabel rightView:self.imageView]; break; case KLXKLayoutButtonStyleUpImageDownTitle: [self layoutVerticalWithUpView:self.imageView downView:self.titleLabel]; break; case KLXKLayoutButtonStyleUpTitleDownImage: [self layoutVerticalWithUpView:self.titleLabel downView:self.imageView]; break; default: break; } } - (void)layoutHorizontalWithLeftView:(UIView *)leftView rightView:(UIView *)rightView { CGRect leftViewFrame = leftView.frame; CGRect rightViewFrame = rightView.frame; CGFloat totalWidth = CGRectGetWidth(leftViewFrame) + self.imageTitleSpacing + CGRectGetWidth(rightViewFrame); leftViewFrame.origin.x = (CGRectGetWidth(self.frame) - totalWidth) / 2.0; leftViewFrame.origin.y = (CGRectGetHeight(self.frame) - CGRectGetHeight(leftViewFrame)) / 2.0; leftView.frame = leftViewFrame; rightViewFrame.origin.x = CGRectGetMaxX(leftViewFrame) + self.imageTitleSpacing; rightViewFrame.origin.y = (CGRectGetHeight(self.frame) - CGRectGetHeight(rightViewFrame)) / 2.0; rightView.frame = rightViewFrame; } - (void)layoutVerticalWithUpView:(UIView *)upView downView:(UIView *)downView { CGRect upViewFrame = upView.frame; CGRect downViewFrame = downView.frame; CGFloat totalHeight = CGRectGetHeight(upViewFrame) + self.imageTitleSpacing + CGRectGetHeight(downViewFrame); upViewFrame.origin.y = (CGRectGetHeight(self.frame) - totalHeight) / 2.0; upViewFrame.origin.x = (CGRectGetWidth(self.frame) - CGRectGetWidth(upViewFrame)) / 2.0; upView.frame = upViewFrame; downViewFrame.origin.y = CGRectGetMaxY(upViewFrame) + self.imageTitleSpacing; downViewFrame.origin.x = (CGRectGetWidth(self.frame) - CGRectGetWidth(downViewFrame)) / 2.0; downView.frame = downViewFrame; } - (void)setImage:(UIImage *)image forState:(UIControlState)state { [super setImage:image forState:state]; [self setNeedsLayout]; } - (void)setTitle:(NSString *)title forState:(UIControlState)state { [super setTitle:title forState:state]; [self setNeedsLayout]; } - (void)setImageTitleSpacing:(CGFloat)imageTitleSpacing{ _imageTitleSpacing = imageTitleSpacing; [self setNeedsLayout]; } - (void)setImageSize:(CGSize)imageSize { _imageSize = imageSize; [self setNeedsLayout]; } @end