UIButton titleLabel imageView位置

2018-12-05  本文已影响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

上一篇 下一篇

猜你喜欢

热点阅读