实现有多种方法:
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;
}