设置button图片和文字的样式

这篇博客介绍了如何创建一个自定义的UIButton子类`ReLayoutBuuton`,通过设置`layoutType`属性实现图片和文字的上图下文、左文右图等布局样式。博客中提供了`.h`和`.m`文件的代码实现,并展示了在ViewController中如何使用这个自定义按钮,特别是设置`layoutType = RelayoutTypeUpDown`来达到上图下文的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建一个继承button的类
.h文件这样写
/*
IB_DESIGNABLE 动态刷新类
IBInspectable 可视化属性
*/

typedef NS_ENUM(NSInteger,RelayoutType) {
/// 系统默认样式
RelayoutTypeNone = 0,
/// 上图下文
RelayoutTypeUpDown = 1,
/// 左文右图
RelayoutTypeRightLeft = 2,
};
NS_ASSUME_NONNULL_BEGIN

@interface ReLayoutBuuton : UIButton
/** 布局样式*/
@property (assign,nonatomic) IBInspectable NSInteger layoutType;
@property (assign,nonatomic) IBInspectable CGFloat margin;
@end

NS_ASSUME_NONNULL_END

.m文件实现

  • (void)setValue:(id)value forUndefinedKey:(NSString *)key
    {
    }

  • (void)layoutSubviews
    {
    [super layoutSubviews];

    if (self.imageView.image == nil || self.titleLabel.text.length == 0) {
    return;
    }

    // 水平调整后
    if (self.titleLabel.center.y == self.imageView.center.y && self.titleLabel.frame.origin.x < self.imageView.frame.origin.x) {
    return;
    }

    // 垂直调整后
    if (self.titleLabel.center.x == self.imageView.center.x) {
    return;
    }

    [self.titleLabel sizeToFit];
    [self.imageView sizeToFit];

    CGRect titleFrame = self.titleLabel.frame;
    CGRect imageFrame = self.imageView.frame;

    CGFloat margin = self.margin;

    CGSize buttonSize = self.bounds.size;

    switch (self.layoutType) {
    case RelayoutTypeNone:

          return;
          break;
      case RelayoutTypeUpDown:
      {
          
          
          margin = margin ? :8;
          CGFloat height = titleFrame.size.height + imageFrame.size.height + margin;
          
          CGFloat imageCenterY = (buttonSize.height - height) * 0.5 + imageFrame.size.height * 0.5;
          self.imageView.center = CGPointMake(buttonSize.width * 0.5, imageCenterY);
          
          
          CGFloat titleCenterY = CGRectGetMaxY(self.imageView.frame) + margin + titleFrame.size.height * 0.5;
          self.titleLabel.center = CGPointMake(buttonSize.width * 0.5, titleCenterY);
      }
          break;
      case RelayoutTypeRightLeft:
      {
          margin = margin ? :5;
          CGFloat totalWidth = titleFrame.size.width + imageFrame.size.width + margin;
          CGFloat titleCenterX = (buttonSize.width - totalWidth) * 0.5 + titleFrame.size.width * 0.5;
          self.titleLabel.center = CGPointMake(titleCenterX, buttonSize.height * 0.5);
    
          
          CGFloat imageCenterX = CGRectGetMaxX(self.titleLabel.frame) + margin + imageFrame.size.width * 0.5;
          
          self.imageView.center = CGPointMake(imageCenterX, buttonSize.height * 0.5);
    
      }
          break;
      default:
          break;
    

    }
    }

然后再要使用的VC导入头文件 #import “ReLayoutBuuton.h”
正常编写button,在需要设置图片和文字上图下文样式 如下设置即可
ReLayoutBuuton * button = [ReLayoutBuuton buttonWithType:UIButtonTypeCustom];
//上图下文
button.layoutType = RelayoutTypeUpDown;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值