自定义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;

    

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序中,可以通过 `navigationStyle` 属性来自定义头部导航栏的样式。该属性可以设置为以下三种取值: - `default`:默认值,显示小程序的默认导航栏。 - `custom`:自定义导航栏,即隐藏小程序的默认导航栏,使用开发者自己定义的导航栏。 - `customCapsule`:自定义导航栏并且包含返回按钮,即隐藏小程序的默认导航栏和返回按钮,使用开发者自己定义的导航栏和返回按钮。 如果选择 `custom` 或 `customCapsule`,则需要在页面的 `json` 文件中定义自己的导航栏。示例: ```json { "navigationBarBackgroundColor": "#fff", "navigationBarTextStyle": "black", "navigationBarTitleText": "自定义导航栏", "navigationStyle": "custom" } ``` 在 `json` 文件中,可以设置导航栏的背景色、文字样式和标题等信息。同时,在对应的 `wxml` 文件中,也需要定义自己的导航栏结构。示例: ```html <view class="nav-bar"> <view class="nav-bar__left"> <image src="/images/back.png"></image> </view> <view class="nav-bar__title">自定义导航栏</view> <view class="nav-bar__right"></view> </view> ``` 在该示例中,使用了一个 `view` 标签作为导航栏的容器,并在其中定义了左侧的返回按钮、中间的标题和右侧的其他内容。通过 CSS 样式可以设置导航栏的背景图片文字颜色等样式。 如果选择 `customCapsule`,则需要在自定义导航栏中添加返回按钮的结构。示例: ```html <view class="nav-bar"> <view class="nav-bar__left"> <button open-type="navigateBack" class="nav-bar__button nav-bar__button--back"> <image src="/images/back.png"></image> </button> </view> <view class="nav-bar__title">自定义导航栏</view> <view class="nav-bar__right"></view> </view> ``` 在该示例中,使用了一个 `button` 标签作为返回按钮,并设置了 `open-type="navigateBack"` 属性来实现返回上一页的功能。同时,在 CSS 样式中也需要对返回按钮进行特殊处理,例如添加圆角等效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值