美文网首页
iOS笔记之UIButton的UIEdgeInsetsMake使

iOS笔记之UIButton的UIEdgeInsetsMake使

作者: 东部落的太阳 | 来源:发表于2018-07-06 10:23 被阅读0次

设置各个值得含义

//UIEdgeInsetsMake(<#T##top: CGFloat##CGFloat#>, <#T##left: CGFloat##CGFloat#>, <#T##bottom: CGFloat##CGFloat#>, <#T##right: CGFloat##CGFloat#>)

testBtn.imageEdgeInsets =UIEdgeInsetsMake(0,10,40,10);

//0 表示据原来的顶部 为0  //10 表示左边框右移 10  (同理 -10 表示左边框左移 10)//40 表示下边框上移 40 //10 表示右边框左移 10 (同理 -10 表示右边框右移 10)

总之:这些参数 正值都是表示 向相反的方向移动相应的距离(例如:对top正值表示向下移动,负值表示向上移动)

这样我们就可以在UIButton的外部轻松设置想要的样式

titleEdgeInsets & imageEdgeInsets

这两个属性的效果是相辅相成的。如果给一个按钮同事设置了title和image,他们默认的状态是图片在左,标题在右,而且image和title之间没有空隙;那就这就引出一个问题,title和image的UIEdgeInsets属性分别的相对于谁而言的?

image的UIEdgeInsets属性的top,left,bottom都是相对于按钮的,right是相对于title;

title的UIEdgeInsets属性的top,bottom,right都是相对于按钮的,left是相对于image;

contentEdgeInsets

我们都知道,UIButton按钮可以只设置一个UILabel或者一个UIImageView,还可以同时具有UILabel和UIImageView;如果给按钮设置contentEdgeInsets属性,就是按钮的内容整体(包含UILabel和UIImageView)进行偏移。

按钮内容整体向右下分别移动10像素:

代码如下

/*

 typedef NS_ENUM(NSUInteger, MKButtonEdgeInsetsStyle) {

 MKButtonEdgeInsetsStyleTop, // image在上,label在下

 MKButtonEdgeInsetsStyleLeft, // image在左,label在右

 MKButtonEdgeInsetsStyleBottom, // image在下,label在上

 MKButtonEdgeInsetsStyleRight // image在右,label在左

 };

 */

//MARK: -定义button相对label的位置

enumYWButtonEdgeInsetsStyle {

    caseTop

    caseLeft

    caseRight

    caseBottom

}

extension UIButton {

    funclayoutButton(style:YWButtonEdgeInsetsStyle, imageTitleSpace:CGFloat) {

        //得到imageView和titleLabel的宽高

        letimageWidth:CGFloat=self.imageView?.frame.size.width??0

        letimageHeight:CGFloat=self.imageView?.frame.size.height??0

        let labelWidth:CGFloat = self.titleLabel?.intrinsicContentSize.width ?? 0

        let labelHeight:CGFloat = self.titleLabel?.intrinsicContentSize.height ?? 0

        //初始化imageEdgeInsets和labelEdgeInsets

        varimageEdgeInsets =UIEdgeInsets.init()

        varlabelEdgeInsets =UIEdgeInsets.init()

        //根据style和space得到imageEdgeInsets和labelEdgeInsets的值

        switchstyle {

        case.Top:

            //上 左 下 右

            imageEdgeInsets =UIEdgeInsets(top: -labelHeight-imageTitleSpace/2, left:0, bottom:0, right: -labelWidth)

            labelEdgeInsets =UIEdgeInsets(top:0, left: -imageWidth, bottom: -imageHeight-imageTitleSpace/2, right:0)

            break

        case.Left:

            imageEdgeInsets =UIEdgeInsets(top:0, left: -imageTitleSpace/2, bottom:0, right: imageTitleSpace)

            labelEdgeInsets =UIEdgeInsets(top:0, left: imageTitleSpace/2, bottom:0, right: -imageTitleSpace/2)

            break

        case.Bottom:

            imageEdgeInsets =UIEdgeInsets(top:0, left:0, bottom: -labelHeight-imageTitleSpace/2, right: -labelWidth)

            labelEdgeInsets =UIEdgeInsets(top: -imageHeight-imageTitleSpace/2, left: -imageWidth, bottom:0, right:0)

            break

        case.Right:

            imageEdgeInsets =UIEdgeInsets(top:0, left: labelWidth+imageTitleSpace/2, bottom:0, right: -labelWidth-imageTitleSpace/2)

            labelEdgeInsets =UIEdgeInsets(top:0, left: -imageWidth-imageTitleSpace/2, bottom:0, right: imageWidth+imageTitleSpace/2)

            break

        }

        self.titleEdgeInsets= labelEdgeInsets

        self.imageEdgeInsets= imageEdgeInsets

    }

}

相关文章

网友评论

      本文标题:iOS笔记之UIButton的UIEdgeInsetsMake使

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