UIButton+direction.m 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // UIButton+direction.m
  3. // xike
  4. //
  5. // Created by 翟玉磊 on 2019/4/12.
  6. // Copyright © 2019 翟玉磊. All rights reserved.
  7. //
  8. #import "UIButton+direction.h"
  9. @implementation UIButton (direction)
  10. - (void)buttonDistance:(CGFloat)distance direction:(directionType)direction{
  11. // self.titleLabel.backgroundColor =[UIColor redColor];
  12. // self.imageView.backgroundColor = [UIColor blueColor];
  13. // NSString *title = self.titleLabel.text;
  14. // CGSize btnLabelsize = [title sizeWithFont:btn.titleLabel.font constrainedToSize:CGSizeMake(MAXFLOAT, btn.titleLabel.frame.size.height)];
  15. // CGSize btnLabelsize =[title boundingRectWithSize:CGSizeMake(MAXFLOAT, self.titleLabel.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil].size;
  16. CGFloat imageWidth = self.imageView.image.size.width;
  17. CGFloat imageHeight = self.imageView.image.size.height;
  18. CGFloat labelWidth = self.titleLabel.frame.size.width;
  19. CGFloat labelHeight = self.titleLabel.frame.size.height;
  20. // CGFloat different = btnLabelsize.width-btn.titleLabel.frame.size.width;
  21. if(direction==0){
  22. [self setTitleEdgeInsets:UIEdgeInsetsMake(0, distance/2.0, 0, -distance/2.0)];
  23. [self setImageEdgeInsets:UIEdgeInsetsMake(0, -distance/2.0, 0, distance/2.0)];
  24. }else if(direction==1){
  25. [self setTitleEdgeInsets:UIEdgeInsetsMake(imageHeight/2.0+distance/2.0, -imageWidth/2.0, -imageHeight/2.0-distance/2.0, imageWidth/2.0)];
  26. [self setImageEdgeInsets:UIEdgeInsetsMake(-labelHeight/2.0-distance/2.0 , labelWidth/2.0, labelHeight/2.0+distance/2.0, -labelWidth/2.0)];
  27. }else if(direction==2){
  28. // 右
  29. [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -(imageWidth+distance/2.0), 0, imageWidth+distance/2.0)];
  30. [self setImageEdgeInsets:UIEdgeInsetsMake(0, labelWidth+distance/2.0, 0, -(labelWidth+distance/2.0))];
  31. }else if(direction==3){
  32. [self setTitleEdgeInsets:UIEdgeInsetsMake(-imageHeight/2.0-distance/2.0, -imageWidth/2.0, imageHeight/2.0+distance/2.0, imageWidth/2.0)];
  33. [self setImageEdgeInsets:UIEdgeInsetsMake(labelHeight/2.0+distance/2.0 , labelWidth/2.0, -labelHeight/2.0-distance/2.0, -labelWidth/2.0)];
  34. }else{
  35. }
  36. }
  37. @end