iOS重写Button
2018-09-28 本文已影响0人
阿木小丸子
- (void)layoutButton:(UIButton*)btn AndBtnImage:(NSString*)img AndBtnTitle:(NSString*)title AndTitleColor:(UIColor*)titleColor AndBackgroundColor:(UIColor*)bgColor{
[btnsetImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
[btnsetTitle:title forState:UIControlStateNormal];
[btnsetTitleColor:titleColor forState:UIControlStateNormal];
btn.backgroundColor= bgColor;
//设置文字偏移:向下偏移图片高度+向左偏移图片宽度 (偏移量是根据[图片]大小来的,这点是关键)
btn.titleEdgeInsets = UIEdgeInsetsMake(btn.imageView.frame.size.height+10, -btn.imageView.frame.size.width, 0, 0);
//设置图片偏移:向上偏移文字高度+向右偏移文字宽度 (偏移量是根据[文字]大小来的,这点是关键)
btn.imageEdgeInsets = UIEdgeInsetsMake(-btn.titleLabel.bounds.size.height-5, 0, 0, -btn.titleLabel.bounds.size.width);
}