美文网首页
UIButton titleLabel imageView位置

UIButton titleLabel imageView位置

作者: 动感超人丶 | 来源:发表于2018-12-05 10:41 被阅读1次
        self.btn.titleLabel.backgroundColor = [UIColor clearColor];
        self.btn.imageView.backgroundColor = [UIColor clearColor];
        
        CGSize titleSize = self.btn.titleLabel.bounds.size;
        CGSize imageSize = self.btn.imageView.bounds.size;
        //    self.btn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 10, 10);
        
        CGFloat spacing = 70;
        // 图左文右
           self.btn.titleEdgeInsets = UIEdgeInsetsMake(0, spacing, 0, 0);
           self.btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, spacing);
        
        // 文左图右
        CGFloat targetTitleX = (self.btn.bounds.size.width - titleSize.width - imageSize.width)/2.0;
        CGFloat targetImageX = targetTitleX+titleSize.width;
    
        self.btn.titleEdgeInsets = UIEdgeInsetsMake(0, targetTitleX - self.btn.titleLabel.frame.origin.x-spacing/2.0, 0, -(targetTitleX - self.btn.titleLabel.frame.origin.x)+spacing/2.0);
        self.btn.imageEdgeInsets = UIEdgeInsetsMake(0, targetImageX-self.btn.imageView.frame.origin.x+spacing/2.0, 0, -(targetImageX-self.btn.imageView.frame.origin.x)-spacing/2.0);
        
        // 图上文下
        self.btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, titleSize.height, -titleSize.width);
        self.btn.titleEdgeInsets = UIEdgeInsetsMake(imageSize.height, -imageSize.width, 0, 0);
    

    不够用就参考https://www.jianshu.com/p/4676d84458f7

    相关文章

      网友评论

          本文标题:UIButton titleLabel imageView位置

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