1、继承UIButton
#import <UIKit/UIKit.h>
@interface MyButton : UIButton
@property(nonatomic,assign) CGRect titleFrame;
@property(nonatomic,assign) CGRect imageFrame;
@end
2、实现父类方法
#import "MyButton.h"
@implementation MyButton{
BOOL isSetImageFrame;
BOOL isSetTitleFrame;
}
-(void)setImageFrame:(CGRect)imageFrame{
isSetImageFrame = YES;
_imageFrame = imageFrame;
}
-(void)setTitleFrame:(CGRect)titleFrame{
isSetTitleFrame = YES;
_titleFrame = titleFrame;
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
if (isSetImageFrame) {
return self.imageFrame;
}else{
return [super imageRectForContentRect:contentRect];
}
}
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
if (isSetTitleFrame) {
return self.titleFrame;
}else{
return [super titleRectForContentRect:contentRect];
}
}
@end
3、通过titleFrame、imageFrame设置title、Image的位置
网友评论