Button上icon加文字

我们常常需要做一个button上面既有icon又有文字。也许左icon右文字,或者上icon下文字。UIButton并没有提供直接这样排布的功能,大家可能也能用

  • 添加一个UIButton的 category
    .h文件

    - (void)titleImageHorizontalAlignmentWithSpace:(float)space;
    - (void)imageTitleHorizontalAlignmentWithSpace:(float)space;
    - (void)titleImageVerticalAlignmentWithSpace:(float)space;
    - (void)imageTitleVerticalAlignmentWithSpace:(float)space;
    
  • .m 文件

    - (void)titleImageHorizontalAlignmentWithSpace:(float)space;
    {
      [self resetEdgeInsets];
      [self setNeedsLayout];
      [self layoutIfNeeded];
    
      CGRect contentRect = [self contentRectForBounds:self.bounds];
      CGSize titleSize = [self titleRectForContentRect:contentRect].size;
      CGSize imageSize = [self imageRectForContentRect:contentRect].size;
    
      [self setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, space)];
      [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -imageSize.width, 0, imageSize.width)];
      [self setImageEdgeInsets:UIEdgeInsetsMake(0, titleSize.width+space, 0, -titleSize.width - space)];
    }
    
    - (void)imageTitleHorizontalAlignmentWithSpace:(float)space;
    {
      [self resetEdgeInsets];
      [self setTitleEdgeInsets:UIEdgeInsetsMake(0, space, 0, -space)];
      [self setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, space)];
    }
    
    - (void)titleImageVerticalAlignmentWithSpace:(float)space;
    {
      [self verticalAlignmentWithTitleTop:YES space:space];
    }
    
    - (void)imageTitleVerticalAlignmentWithSpace:(float)space;
    {
        [self verticalAlignmentWithTitleTop:NO space:space];
    }
    
    - (void)verticalAlignmentWithTitleTop:(BOOL)isTop space:(float)space ;
    {
        [self resetEdgeInsets];
        [self setNeedsLayout];
        [self layoutIfNeeded];
    
        CGRect contentRect = [self contentRectForBounds:self.bounds];
        CGSize titleSize = [self titleRectForContentRect:contentRect].size;
        CGSize imageSize = [self imageRectForContentRect:contentRect].size;
    
        float halfWidth = (titleSize.width + imageSize.width)/2;
        float halfHeight = (titleSize.height + imageSize.height)/2;
    
        float topInset = MIN(halfHeight, titleSize.height);
        float leftInset = (titleSize.width - imageSize.width)>0?(titleSize.width - imageSize.width)/2:0;
        float bottomInset = (titleSize.height - imageSize.height)>0?(titleSize.height - imageSize.height)/2:0;
        float rightInset = MIN(halfWidth, titleSize.width);
    
        if (isTop) {
          [self setTitleEdgeInsets:UIEdgeInsetsMake(-titleSize.height-space, - halfWidth, imageSize.height+space, halfWidth)];
          [self setContentEdgeInsets:UIEdgeInsetsMake(topInset+space, leftInset, -bottomInset, -rightInset)];
      } else {
      [self setTitleEdgeInsets:UIEdgeInsetsMake(imageSize.height+space, - halfWidth, -titleSize.height-space, halfWidth)];
      [self setContentEdgeInsets:UIEdgeInsetsMake(-bottomInset, leftInset, topInset+space, -rightInset)];
      }
    }
    
    - (void)resetEdgeInsets
    {
      [self setContentEdgeInsets:UIEdgeInsetsZero];
      [self setImageEdgeInsets:UIEdgeInsetsZero];
      [self setTitleEdgeInsets:UIEdgeInsetsZero];
    }    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容