Btn titleLabel与imageView位置调整

实现有多种方法:

1 方法一

   [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

    [btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0 )];

用下面方法计算btn宽度。

这个做可以实现 文字在左,图片在右。

不过当需要与某个控件对齐就懵逼了。

网上查 用这个

btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

然而并没什卵用。

 

2 方法二

新建一个类 custBtn 然后对里面重新布局下

custBtn 继承UIButton

在- (void)layoutSubviews;方法中对子空间布局

 

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {

        
    }
    return self;
}

-(void)layoutSubviews
{
    
    [super layoutSubviews];
    /** 修改 title 的 frame */
    // 1.获取 titleLabel 的 frame
    CGRect titleLabelFrame = self.titleLabel.frame;
    // 2.修改 titleLabel 的 frame
    titleLabelFrame.origin.x = 0;
    // 3.重新赋值
    self.titleLabel.frame = titleLabelFrame;
    
    /** 修改 imageView 的 frame */
    // 1.获取 imageView 的 frame
    CGRect imageViewFrame = self.imageView.frame;
    // 2.修改 imageView 的 frame
    imageViewFrame.origin.x = titleLabelFrame.size.width+5;
    // 3.重新赋值
    self.imageView.frame = imageViewFrame;
}

//上面方法是上图的主要方法。 使用同UIButton

NSString *titleStr = @"根据字体字号根据字体字号";
    CGFloat btnw = [self getLabelHeightWithText:titleStr width:200 font:14];
    
    CGSize textSize1 = [titleStr sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];
    
    NSLog(@"======== %f--%f", btnw,textSize1.width);
    cusBtn = [CustomBtn buttonWithType:(UIButtonTypeCustom)];
    cusBtn.frame = CGRectMake(50, 100, textSize1.width+30, 30);
    cusBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [cusBtn setTitle:titleStr forState:(UIControlStateNormal)];
    [cusBtn setImage:[UIImage imageNamed:@"down.png"] forState:(UIControlStateNormal)];
    [self.view addSubview:cusBtn];
    cusBtn.backgroundColor =[UIColor redColor];
    [cusBtn addTarget:self action:@selector(customBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];

//图片在右
- (void)layoutSubviews{
    [super layoutSubviews];
    self.titleLabel.x = 10;
    self.imageView.x = CGRectGetMaxX(self.titleLabel.frame) + 10;
}


//图片在上

- (void)layoutSubviews{
    [super layoutSubviews];
   
    self.imageView.top = 10;
    self.imageView.centerX = self.centerX;
    
    self.titleLabel.left = 0;
    self.titleLabel.width = self.width;
     self.titleLabel.top =CGRectGetMaxY(self.imageView.frame) +  10;
}

3 方法三

#import "CustomBtn.h"

@implementation CustomBtn{
    CGFloat ww ,hh;
   UILabel *contentLa;
   UIImageView *imageV;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
//        ww = super.frame.size.width;
//        hh = super.frame.size.height;
//        
//       contentLa =[[UILabel alloc]init];
//       contentLa.frame =CGRectMake(0, 0, ww-15, hh);
//        [self addSubview:contentLa];
//        contentLa.textAlignment = NSTextAlignmentLeft;
//        contentLa.text = self.titleLabel.text;
//        contentLa.textColor = self.titleLabel.textColor;
//        imageV = [[UIImageView alloc]init];
//        imageV.frame = CGRectMake(ww-10,  hh/2-10/2, 10, 10);
//        [self addSubview:imageV];
//        imageV.image = self.imageView.image;
        
        
    }
    return self;
}

在btn中自己添加Lable 和 imageView

    这个可实现左对齐

//根据高度度求宽度  text 计算的内容  Height 计算的高度 font字体大小

-(CGFloat)getLabelHeightWithText:(NSString *)text width:(CGFloat)width font: (CGFloat)font

{

    NSDictionary *attrs = @{NSFontAttributeName : [UIFont systemFontOfSize:font]};

    CGSize textSize = [text boundingRectWithSize:CGSizeMake(200, 35) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;

    

    return textSize.width;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值