swift UIButton-图文排列

swift 里没有OC的分类概念,但可以用extension为类拓展方法。

下面的extension 为UIButton提供了四种样式:
图上文下,图下文上,图左文右(UIButton默认样式),图右文左。
可以通过space设置图文之间的间距

enum LayoutStyle: Int {
   case imageTop
   case imageLeft
   case imageBottom
   case imageRight
}

extension UIButton {
   
   func layoutButton(_ style: LayoutStyle, space: CGFloat) {
       
       guard let titleL = self.titleLabel, let imageV = self.imageView else {
           return
       }
       
       let imageWidth = imageV.frame.size.width
       let imageHeight = imageV.frame.size.height
       
       let labelWidth = titleL.frame.size.width
       let labelHeight = titleL.frame.size.height
       
       let imageX = imageV.frame.origin.x
       let labelX = titleL.frame.origin.x
       
       let margin = labelX - imageX - imageWidth
       
       var imageInsets = UIEdgeInsets.zero
       var labelInsets = UIEdgeInsets.zero
       
       /**
        *  titleEdgeInsets是title相对于其上下左右的inset
        *  如果只有title,那它上下左右都是相对于button的,image也是一样;
        *  如果同时有image和label,那这时候image的上左下是相对于button,右边是相对于label的;title的上右下是相对于button,左边是相对于image的。
        */
       switch style {
       case .imageTop:
           imageInsets = UIEdgeInsets(top: -0.5 * labelHeight, left: 0.5 * labelWidth + 0.5 * margin + imageX, bottom: 0.5 * (labelHeight + space), right: 0.5 * (labelWidth - margin))
           labelInsets = UIEdgeInsets(top: 0.5 * (imageHeight + space), left: -(imageWidth - 5), bottom: -0.5 * imageHeight, right: 5)
           
       case .imageBottom:
           imageInsets = UIEdgeInsets(top: 0.5 * (labelHeight + space), left: 0.5 * labelWidth + imageX, bottom: -0.5 * labelHeight, right: 0.5 * labelWidth)
           labelInsets = UIEdgeInsets(top: -0.5 * imageHeight, left: -(imageWidth - 5), bottom:0.5 * (imageHeight + space), right: 5)
           
       case .imageRight:
           imageInsets = UIEdgeInsets(top: 0, left: 0.5 * (labelWidth + space), bottom: 0, right: -(labelWidth + 0.5 * space))
           labelInsets = UIEdgeInsets(top: 0, left: -(imageWidth + 0.5 * space), bottom: 0, right: imageWidth + space * 0.5)
           
       default:
           imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0.5 * space)
           labelInsets = UIEdgeInsets(top: 0, left: 0.5 * space, bottom: 0, right: 0)
       }
       
       self.titleEdgeInsets = labelInsets;
       self.imageEdgeInsets = imageInsets;
   }
   
}



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,142评论 25 708
  • 导语 系统的 UIButton 默认状态下的样式是图标在左标题在右,但有时候可能需要不同的排版。当然可以通过继承添...
    iOSugarCom阅读 3,149评论 1 2
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,796评论 1 92
  • 生存,还是毁灭没有中间状态。这就类似于生活中的矛盾着,想吃米饭,又想吃馒头,迟迟不做决定。在工作中也有很多类似的事...
    临淄茂业DDM王春梅阅读 311评论 0 0
  • 《每天砰然心动的整理魔法》语录整理,字字精华。透过物品与自己对话,只留下让你砰然心动! 第一章 与自己对话 度过令...
    亦兮林阅读 2,838评论 4 43