关于UIbutton的总结有很多这篇博文不错 推荐一下
http://www.cnblogs.com/ygm900/archive/2013/05/28/3104169.html
简单的搞两个btn的效果

一般button 加文字是这样的
但是有时候我们想要文字和图片搁一边的如图

这样改遍图文布局就行
上代码
#import "MyButton.h"
@implementation MyButton
-(CGRect)titleRectForContentRect:(CGRect)contentRect
{//上下
// CGFloat titleY = contentRect.size.height *0.6;
//
// CGFloat titleW = contentRect.size.width;
//
// CGFloat titleH = contentRect.size.height - titleY;
//左右
CGFloat titleY = 0;
CGFloat titleW = contentRect.size.width*0.5;
CGFloat titleH = contentRect.size.height;
//不规则
// CGFloat titleY = 0;
//
// CGFloat titleW = contentRect.size.width;
//
// CGFloat titleH = contentRect.size.height;
return CGRectMake(0, titleY, titleW, titleH);
}
-(CGRect)imageRectForContentRect:(CGRect)contentRect
{
//上下
// CGFloat imageW = CGRectGetWidth(contentRect);
// CGFloat imageH = contentRect.size.height * 0.6;
//左右
CGFloat imageX = contentRect.size.width*0.5;
CGFloat imageW = CGRectGetWidth(contentRect) * 0.5;
CGFloat imageH = contentRect.size.height;
//不规则
// CGFloat imageX = contentRect.size.width*0.3;
// CGFloat imageW = CGRectGetWidth(contentRect) * 0.4;
//
// CGFloat imageH = contentRect.size.height;
return CGRectMake(imageX, 0, imageW, imageH);
}
@end
重写以上代码就可以实现 其中的位置自己修改就好
那么问题来了如果我想实现这正效果怎么办

btn 中间视图两边是文字怎么搞 很简单直接重写btn就好
#import <UIKit/UIKit.h>
//继承btn 的.h
@interface MyButton : UIButton
@property (nonatomic,strong)UILabel * firstLabel;
@property (nonatomic,strong)UILabel * secondLabel;
@property (nonatomic,strong)UIImageView * centerImage;
- (instancetype)initWithFrame:(CGRect)frame;
@end
//.m文件
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
UILabel * lalbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 20, frame.size.height)];
self.firstLabel = lalbl;
[self addSubview:lalbl];
UIImageView * imagebtn = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lalbl.frame), 0, frame.size.width - 40, frame.size.height)];
self.centerImage = imagebtn;
[self addSubview:imagebtn];
UILabel * lalbl2 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imagebtn.frame), 0, 20, frame.size.height)];
self.secondLabel = lalbl2;
[self addSubview:lalbl2];
}
return self;
}
那么如何调用呢
MyButton * mybtn = [[MyButton alloc]initWithFrame:CGRectMake(200, 200, 100, 50)];
mybtn.firstLabel.text = @"高";
mybtn.secondLabel.text = @"ff";
mybtn.centerImage.image = [UIImage imageNamed:@"icon120×120-1"] ;
[mybtn addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:mybtn];
应该不用解释 了 最重要的是看链接 内容很多
网友评论