iOS自定义UIButton中Image和title的位置
2016-05-31 本文已影响245人
为之则易ing
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