自定义button同时显示文字和图片

代码搬运工这样就可以实现图片和文字同时显示


首先创建一个继承于UIButton的类

//1. .h中这样写

#import <UIKit/UIKit.h>


@interface SHJF_Button : UIButton


@end



//2. .m中这样写

#import "SHJF_Button.h"


@implementation SHJF_Button


重写init方法

- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        //可根据自己的需要随意调整

        

        self . titleLabel . textAlignment = NSTextAlignmentLeft ;

        

        self . titleLabel . font =[ UIFont systemFontOfSize : 14.0 ];

        //        self . titleLabel . backgroundColor = [UIColor orangeColor];

        

        //        self . imageView . contentMode = UIViewContentModeLeft ;

    }

    return self;

}

//重写父类UIButton的方法


//更具buttonrect设定并返回文本labelrect


- ( CGRect )titleRectForContentRect:( CGRect )contentRect


{


    CGFloat titleW = myWidth/3/5*3 ;

    

    CGFloat titleH = myHeight/100*3 ;

    

    CGFloat titleX = myWidth/3/5 ;

    

    CGFloat titleY = myHeight/100*12.4 ;

    

    

    

    contentRect = ( CGRect ){{titleX,titleY},{titleW,titleH}};

    

    return contentRect;

    

}


//更具buttonrect设定并返回UIImageViewrect


- ( CGRect )imageRectForContentRect:( CGRect )contentRect


{

    


    

    CGFloat imageW = myWidth/100*10 ;

    

    CGFloat imageH = myWidth/100*10 ;

    

    CGFloat imageX = myWidth/100*11.5 ;

    

    CGFloat imageY = myHeight/100*4.1 ;

    contentRect = ( CGRect ){{imageX,imageY},{imageW,imageH}};

    

    return contentRect;

    

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值