UIButton上文字和图片位置多种样式

1.上边图片, 下方文字

- (UIButton *)buttonWithFrame:(CGRect)frame imageName:(NSString *)imageName title:(NSString *)title tag:(int)tag{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = frame;

UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height/3*2)];
icon.contentMode = UIViewContentModeScaleAspectFit;
icon.image = [UIImage imageNamed:imageName];
[btn addSubview:icon];

[btn setTitle:title forState:UIControlStateNormal];
[btn setTitleColor:[UIColor colorWithHexString:@"363636"] forState:UIControlStateNormal];
//2018年12月29日11:26:47
btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
btn.tag = tag;
[btn setTitleEdgeInsets:UIEdgeInsetsMake(frame.size.height*4.0/5.0, 0, 0, 0)];
[btn addTarget:self action:@selector(itembtnClick:) forControlEvents:UIControlEventTouchUpInside];

return btn;
 }

2.左边文字, 有边图片

 - (UIButton *)buttonWithFrame:(CGRect)frame title:(NSString 
*)title imageName:(NSString *)imageName textColor:(UIColor 
*)textcolor tag:(int)tag superView :(UIView *)superView
{
UIButton *btn = [[UIButton alloc]initWithFrame:frame];
[btn setTitle:title forState:UIControlStateNormal];

[btn setImage:[UIImage imageNamed:@"mine_icon_front"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"mine_icon_front"] forState:UIControlStateSelected];

[btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -btn.imageView.size.width, 0, btn.imageView.size.width)];
[btn setImageEdgeInsets:UIEdgeInsetsMake(0, btn.titleLabel.bounds.size.width, 0, -btn.titleLabel.bounds.size.width)];
btn.titleLabel.font = [UIFont systemFontOfSize:12];
btn.tag = tag;
[btn addTarget:self action:@selector(calendarSelect) forControlEvents:UIControlEventTouchUpInside];
[superView addSubview:btn];
return btn;

}

你可能感兴趣的:(UIButton上文字和图片位置多种样式)