//
// ZXSTopImageTextbutton.h
// ZXSOCAPP
//
// Created by bi xu on 2018/6/1.
// Copyright © 2018年 CoderZXS. All rights reserved.
// 使用继承创建自定义按钮,图片在上,标题在下
#import <UIKit/UIKit.h>
@interface ZXSTopImageTextbutton : UIButton
- (instancetype)zxs_initWithFrame:(CGRect)frame image:(UIImage *)image target:(id)target action:(SEL)action title:(NSString *)title;
@end
-------------------------------------------------------------------
//
// ZXSTopImageTextbutton.m
// ZXSOCAPP
//
// Created by bi xu on 2018/6/1.
// Copyright © 2018年 CoderZXS. All rights reserved.
#import "ZXSTopImageTextbutton.h"
@implementation ZXSTopImageTextbutton
#pragma mark - 系统方法
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat buttonWidth = self.frame.size.width;
CGFloat buttonHeight = self.frame.size.height;
self.imageView.frame = CGRectMake((buttonWidth * 0.2), 0, (buttonWidth * 0.6), (buttonWidth * 0.6));
CGFloat titleLabelY = (CGRectGetMaxY(self.imageView.frame) + buttonHeight * 0.2);
self.titleLabel.frame = CGRectMake(0, titleLabelY, buttonWidth, (buttonHeight - titleLabelY));
}
#pragma mark - 自定义方法
- (instancetype)zxs_initWithFrame:(CGRect)frame image:(UIImage *)image target:(id)target action:(SEL)action title:(NSString *)title {
if (self == [super initWithFrame:frame]) {
[self setImage:image forState:UIControlStateNormal];
[self setTitle:title forState:UIControlStateNormal];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
[self addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
@end
网友评论