YHWorkGroupButton.m 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // YHWorkGroupButton.m
  3. // HKP
  4. // CSDN: http://blog.csdn.net/samuelandkevin
  5. // Created by kun on 16/5/14.
  6. // Copyright © 2016年 HKP. All rights reserved.
  7. //
  8. #import "YHWorkGroupButton.h"
  9. static CGFloat const imgW = 13;
  10. static CGFloat const imgH = 13;
  11. static CGFloat const margin = 5; //图片与文字的间距
  12. static CGFloat const labelH = 13;
  13. @interface YHWorkGroupButton()
  14. @end
  15. @implementation YHWorkGroupButton
  16. /*
  17. // Only override drawRect: if you perform custom drawing.
  18. // An empty implementation adversely affects performance during animation.
  19. - (void)drawRect:(CGRect)rect {
  20. // Drawing code
  21. }
  22. */
  23. - (instancetype)initWithFrame:(CGRect)frame
  24. {
  25. self = [super initWithFrame:frame];
  26. if (self)
  27. {
  28. CGFloat font = 12.0f;
  29. self.imageView.contentMode = UIViewContentModeScaleAspectFit;
  30. self.titleLabel.textAlignment = NSTextAlignmentLeft;
  31. [self setTitleColor:RGBCOLOR(151, 161, 173) forState:UIControlStateNormal];
  32. [self setTitleColor:RGBCOLOR(0, 191, 143) forState:UIControlStateSelected];
  33. [self.titleLabel setTintColor:[UIColor colorWithRed:122/255.0 green:122/255.0 blue:122/255.0 alpha:1.0]];
  34. [self.titleLabel setFont:[UIFont boldSystemFontOfSize:font]];
  35. self.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  36. }
  37. return self;
  38. }
  39. //1.重写方法,改变图片的位置,在titleRect..方法后执行
  40. - (CGRect)imageRectForContentRect:(CGRect)contentRect
  41. {
  42. CGFloat imageX = self.bounds.size.width*0.4;
  43. CGFloat imageY = self.center.y - imgH/2;
  44. CGFloat width = imgW;
  45. CGFloat height = imgH;
  46. return CGRectMake(imageX, imageY, width, height);
  47. }
  48. //2.改变title文字的位置,构造title的矩形即可
  49. - (CGRect)titleRectForContentRect:(CGRect)contentRect
  50. {
  51. CGFloat titleX = CGRectGetMaxX(self.imageView.frame) + margin;
  52. CGFloat titleY = self.center.y - labelH/2;
  53. CGFloat width = self.frame.size.width - titleX;
  54. CGFloat height = labelH;
  55. return CGRectMake(titleX, titleY, width, height);
  56. }
  57. - (void)setHighlighted:(BOOL)highlighted{
  58. }
  59. @end