iOS UIbutton修改文字和图片布局

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

DC969018-4CEC-435C-87ED-E6C2DBAA5324.png

一般button 加文字是这样的

但是有时候我们想要文字和图片搁一边的如图

C948FC82-FF1D-4C7E-BDD8-27934F8AB9F0.png

这样改遍图文布局就行
上代码

#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

重写以上代码就可以实现 其中的位置自己修改就好

那么问题来了如果我想实现这正效果怎么办


450B43ED-815C-4DF5-BB23-61F361327D73.png

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];

应该不用解释 了 最重要的是看链接 内容很多

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,964评论 25 709
  • UIButton内部文本和图片的布局是我们日常代码中,不可缺少的部分,按钮默认左边图片右边文本,那要实现左边文本,...
    Lsx_f阅读 4,763评论 1 2
  • 1 在男生的世界里,你爱我就不会走;在女生的世界里,你爱我就会来找我。 所以,最后你以为我还会回来、我也以为你会来...
    闰润阅读 3,870评论 11 12
  • 前段时间碰到一个姑娘问我婚姻的相关事宜,其实对于婚姻这种事我也是井底之蛙,分不清爱情是非,而且本着帮和不帮分的心态...
    周维格阅读 4,655评论 6 20
  • 今天想聊聊般配,般配的友情,也许也是爱情! 记得大学时代,有个稍显粗壮大只的女生和一个稍显瘦弱小个的男生,最常见的...
    秒睡神阅读 1,841评论 0 0

友情链接更多精彩内容