方法一:通过storyboard属性栏设置
1.给button设置图片和文字
2.点击button右侧属性
3.设置最下面content title image 的左右edge边距调整button内部图片和文字的frame
方法二:添加分类
由于button继承自uiview,重写属性的set get方法
在button的方法中
创建分类步骤
/**
* 分类能做什么事情
*
* 扩充方法
*
* 可以添加属性
不在分类中使用@property 会做哪三件事?
1.创建一个下划线的成员变量
2.set方法
3.get方法
*/
#import <UIKit/UIKit.h>
@interface UIView (Extension)
/**
* 在分类中使用@property 不会做上面的三件事
*
*/
//有一个x 属性
@property (nonatomic , assign)CGFloat x;
@property (nonatomic , assign)CGFloat y;
@property (nonatomic , assign)CGFloat w;
@property (nonatomic , assign)CGFloat h;
#import "UIView+Extension.h"
@implementation UIView (Extension)
//创建setX 方法
-(void)setX:(CGFloat)x
{
CGRect rect = self.frame;
rect.origin.x = x;
self.frame = rect;
}
-(CGFloat)x
{
return self.frame.origin.x;
}
-(void)setY:(CGFloat)y
{
CGRect rect = self.frame;
rect.origin.y = y;
self.frame = rect;
}
-(CGFloat)y
{
return self.frame.origin.y;
}
-(void)setW:(CGFloat)w
{
CGRect rect = self.frame;
rect.size.width = w;
self.frame = rect;
}
-(CGFloat)w
{
return self.frame.size.width;
}
-(void)setH:(CGFloat)h
{
CGRect rect = self.frame;
rect.size.height = h;
self.frame = rect;
}
-(CGFloat)h
{
return self.frame.size.height;
}