美文网首页
调整button图文排布

调整button图文排布

作者: 口厄 | 来源:发表于2019-04-03 16:05 被阅读0次

- (void)imagePosition:(BSYButtonImageEdgeInsetsStyle)style space:(CGFloat)space {

    if(self.imageView==nil) {

        return;

    }

    if(self.titleLabel==nil) {

        return;

    }

    //获取图像的宽和高

    CGFloat imageWidth = self.imageView.frame.size.width;

    CGFloat imageHeight = self.imageView.frame.size.height;

    //获取文字的宽和高

    CGFloatlabelWidth  =self.titleLabel.intrinsicContentSize.width;

    CGFloat labelHeight = self.titleLabel.intrinsicContentSize.height;

    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;

    UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;

    //UIButton同时有图像和文字的正常状态---左图像右文字,间距为0

    switch(style) {

        caseLEFT:

            imageEdgeInsets =UIEdgeInsetsMake(0, -space*0.5,0, space*0.5);

            labelEdgeInsets =UIEdgeInsetsMake(0, space*0.5,0, -space*0.5);

            break;

        caseRIGHT:

            imageEdgeInsets =UIEdgeInsetsMake(0,labelWidth + space *0.5,0, -labelWidth - space *0.5);

            labelEdgeInsets =UIEdgeInsetsMake(0,  -imageWidth - space *0.5,0, imageWidth + space *0.5);

            break;

        caseTOP:

            imageEdgeInsets =UIEdgeInsetsMake( -imageHeight *0.5- space *0.5,labelWidth *0.5, imageHeight *0.5+ space *0.5, -labelWidth *0.5);

            labelEdgeInsets =UIEdgeInsetsMake(labelHeight *0.5+ space *0.5, -imageWidth *0.5, -labelHeight *0.5- space *0.5, imageWidth *0.5);

            break;

        caseBOTTOM:

            imageEdgeInsets =UIEdgeInsetsMake(imageHeight *0.5+ space *0.5,labelWidth *0.5, -imageHeight *0.5- space *0.5,-labelWidth *0.5);

            labelEdgeInsets =UIEdgeInsetsMake(-labelHeight *0.5- space *0.5, -imageWidth *0.5, labelHeight *0.5+ space *0.5, imageWidth *0.5);

            break;

        default:

            break;

    }

    self.titleEdgeInsets= labelEdgeInsets;

    self.imageEdgeInsets= imageEdgeInsets;

}

相关文章

网友评论

      本文标题:调整button图文排布

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