美文网首页
iOS重写Button

iOS重写Button

作者: 阿木小丸子 | 来源:发表于2018-09-28 18:11 被阅读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);

}

相关文章

网友评论

      本文标题:iOS重写Button

      本文链接:https://www.haomeiwen.com/subject/yvnpoftx.html