SortButton.m 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // SortButton.m
  3. // iSales-NPD
  4. //
  5. // Created by Jack on 16/9/6.
  6. // Copyright © 2016年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "SortButton.h"
  9. @implementation SortButton
  10. - (instancetype)initWithFrame:(CGRect)frame {
  11. if (self = [super initWithFrame:frame]) {
  12. [self addSubview:self.titleLabel];
  13. [self addSubview:self.imageView];
  14. }
  15. return self;
  16. }
  17. + (SortButton*)sortButtonWithHeight:(CGFloat)height {
  18. SortButton *btn = [[SortButton alloc] initWithFrame:CGRectMake(10, 0, 85, height)];
  19. return btn;
  20. }
  21. - (UILabel *)titleLabel {
  22. if (!_titleLabel) {
  23. CGSize size = [self string:@"sort by" sizeWithSize:CGSizeMake(60, 22) font:[UIFont systemFontOfSize:17]];
  24. CGFloat h = CGRectGetHeight(self.bounds);
  25. _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, (h - size.height) / 2, size.width, size.height)];
  26. _titleLabel.font = [UIFont systemFontOfSize:17];
  27. _titleLabel.textColor = [UIColor blackColor];
  28. _titleLabel.text = @"Sort By";
  29. [_titleLabel sizeToFit];
  30. }
  31. return _titleLabel;
  32. }
  33. - (UIImageView *)imageView {
  34. if (!_imageView) {
  35. CGFloat h = CGRectGetHeight(self.bounds);
  36. CGFloat x = CGRectGetMaxX(self.titleLabel.frame) + 5;
  37. CGFloat y = (h - 22) / 2;
  38. _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, 22, 22)];
  39. _imageView.image = [UIImage imageNamed:@"DX_22"];
  40. _imageView.userInteractionEnabled = YES;
  41. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImageView:)];
  42. [_imageView addGestureRecognizer:tap];
  43. }
  44. return _imageView;
  45. }
  46. -(CGSize)string:(NSString *)string sizeWithSize:(CGSize)size font:(UIFont *)font{
  47. NSDictionary *attribute = @{NSFontAttributeName: font};
  48. CGSize resSize = [string boundingRectWithSize:size
  49. options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
  50. attributes:attribute
  51. context:nil].size;
  52. return resSize;
  53. }
  54. - (void)clickImageView:(id)recognizer {
  55. [self sendActionsForControlEvents:UIControlEventTouchUpInside];
  56. }
  57. @end