财神随手记账

TopImage_BottomTitleLabel_ContentCenter_Button.m 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // TopImage_BottomTitleLabel_ContentCenter_Button.m
  3. // ContentCenterButton
  4. //
  5. // Created by foolsparadise on 27/01/2018.
  6. // Copyright © 2018 github.com/foolsparadise All rights reserved.
  7. //
  8. #import "TopImage_BottomTitleLabel_ContentCenter_Button.h"
  9. @implementation TopImage_BottomTitleLabel_ContentCenter_Button
  10. /*
  11. // Only override drawRect: if you perform custom drawing.
  12. // An empty implementation adversely affects performance during animation.
  13. - (void)drawRect:(CGRect)rect {
  14. // Drawing code
  15. }
  16. */
  17. - (instancetype)initWithFrame:(CGRect)frame
  18. {
  19. self = [super initWithFrame:frame];
  20. if (self) {
  21. [self setupUI];
  22. }
  23. return self;
  24. }
  25. - (void)setupUI {
  26. //self.backgroundColor = [UIColor orangeColor];
  27. //self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@""]];
  28. //self.contentMode = UIViewContentModeScaleAspectFit;
  29. //self.layer.cornerRadius = 68/2.0;
  30. self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  31. self.layer.masksToBounds = YES;
  32. //self.tag = 0;
  33. self.titleLabel.font = [UIFont systemFontOfSize:14];
  34. [self setTitleColor:[UIColor colorWithRed:188/255.0 green:186/255.0 blue:186/255.0 alpha:1] forState:UIControlStateNormal];
  35. //[self setTitle:@"" forState:UIControlStateNormal];
  36. //[self setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
  37. }
  38. - (void)configureheightSpace:(CGFloat)heightSpace withTitle:(NSString *)title withImageNamed:(NSString *)imagename
  39. {
  40. [self setTitle:title forState:UIControlStateNormal];
  41. [self setImage:[UIImage imageNamed:imagename] forState:UIControlStateNormal];
  42. //居中设置
  43. CGSize imgViewSize,titleSize,btnSize;
  44. UIEdgeInsets imageViewEdge,titleEdge;
  45. //CGFloat heightSpace = 26.0f; //(整个button大小120-其中image大小68)/2=26算出来的
  46. //设置按钮内边距
  47. imgViewSize = self.imageView.bounds.size;
  48. titleSize = self.titleLabel.bounds.size;
  49. btnSize = self.bounds.size;
  50. imageViewEdge = UIEdgeInsetsMake(heightSpace,heightSpace, btnSize.height -imgViewSize.height - heightSpace, - titleSize.width);
  51. [self setImageEdgeInsets:imageViewEdge];
  52. titleEdge = UIEdgeInsetsMake(imgViewSize.height +heightSpace, - imgViewSize.width, 0.0, 0.0);
  53. [self setTitleEdgeInsets:titleEdge];
  54. }
  55. @end