第二十篇、自定义UIButton(设置title和image的位置)

时间:2023-03-09 08:09:33
第二十篇、自定义UIButton(设置title和image的位置)
#import "CustomButton.h"
#define ImageW 15
#define ImageH 15
#define KRadio 0.75 @implementation CustomButton -(instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super initWithCoder:aDecoder]) { //设置图片显示的样式
//self.imageView.contentMode = UIViewContentModeCenter;
//self.titleLabel.textAlignment = NSTextAlignmentCenter;
}
return self;
} -(CGRect)titleRectForContentRect:(CGRect)contentRect{
CGFloat titleW = contentRect.size.width * KRadio;
CGFloat titleH = contentRect.size.height;
return CGRectMake(, , titleW, titleH);
} -(CGRect)imageRectForContentRect:(CGRect)contentRect{ CGFloat imageW = ImageW;
CGFloat imageH = ImageH;
CGFloat imageY = (contentRect.size.height-imageH) / 2.0;
CGFloat imageX = contentRect.size.width * KRadio;
return CGRectMake(imageX,imageY, imageW, imageH); // CGFloat imageW = ImageW;
// CGFloat imageH = contentRect.size.height;
// CGFloat imageX = contentRect.size.width-ImageW;
// return CGRectMake(imageX,0, imageW, imageH);
} -(void)setHighlighted:(BOOL)highlighted{
} //- (IBAction)clickTitle:(UIButton *)sender {
//
// [UIView animateWithDuration:0.25 animations:^{
// if (CGAffineTransformIsIdentity(sender.imageView.transform)) {
// sender.imageView.transform = CGAffineTransformMakeRotation(M_PI);
// }else{
// sender.imageView.transform = CGAffineTransformIdentity;
// }
//
// }];
//
//} @end