VerticalButton.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // SUVerticalButton.m
  3. //
  4. //
  5. // Created by stcui on 15/9/1.
  6. //
  7. //
  8. #import "VerticalButton.h"
  9. static const CGFloat kPadding = 2;
  10. @implementation VerticalButton
  11. - (instancetype)initWithTitle:(NSString *)title
  12. {
  13. if (self = [self init]) {
  14. [self setTitle:title forState:UIControlStateNormal];
  15. self.titleLabel.adjustsFontSizeToFitWidth = YES;
  16. self.titleLabel.minimumScaleFactor = 0.3;
  17. self.imageView.contentMode = UIViewContentModeScaleAspectFit;
  18. }
  19. return self;
  20. }
  21. - (void)awakeFromNib
  22. {
  23. [super awakeFromNib];
  24. self.titleLabel.adjustsFontSizeToFitWidth = YES;
  25. self.titleLabel.minimumScaleFactor = 0.3;
  26. }
  27. - (CGSize)sizeThatFits:(CGSize)size
  28. {
  29. NSDictionary *attr = @{NSFontAttributeName: self.titleLabel.font};
  30. CGSize textSize = [[self titleForState:self.state] sizeWithAttributes:attr];
  31. CGSize imageSize = [self imageForState:self.state].size;
  32. CGFloat width = MAX(textSize.width, imageSize.width);
  33. CGFloat height = textSize.height + imageSize.height + kPadding;
  34. return CGSizeMake(truncf(width), truncf(height));
  35. }
  36. - (void)layoutSubviews
  37. {
  38. [super layoutSubviews];
  39. NSDictionary *attr = @{NSFontAttributeName: self.titleLabel.font};
  40. CGSize textSize = [[self titleForState:self.state] sizeWithAttributes:attr];
  41. CGFloat width = CGRectGetWidth(self.bounds);
  42. if (textSize.width > width) {
  43. textSize.width = width;
  44. }
  45. CGSize imageSize = [self imageForState:self.state].size;
  46. CGFloat totalHeight = textSize.height + imageSize.height + kPadding;
  47. CGFloat centerX = CGRectGetMidX(self.bounds);
  48. self.imageView.center = CGPointMake(centerX, (CGRectGetHeight(self.bounds) - totalHeight) / 2 + imageSize.height / 2);
  49. CGRect imageFrame = self.imageView.frame;
  50. BOOL changed = NO;
  51. if (imageFrame.origin.x < 0) {
  52. imageFrame.size.width += 2*imageFrame.origin.x;
  53. imageFrame.origin.x = 0;
  54. changed = YES;
  55. }
  56. if (imageFrame.origin.y < 0) {
  57. imageFrame.size.height += 2*imageFrame.origin.y;
  58. imageFrame.origin.y = 0;
  59. changed = YES;
  60. }
  61. BOOL imageIsSmaller = imageSize.width <= CGRectGetWidth(imageFrame) && imageSize.height <= CGRectGetHeight(imageFrame);
  62. self.imageView.contentMode = imageIsSmaller ? UIViewContentModeCenter : UIViewContentModeScaleAspectFit;
  63. if (changed) {
  64. self.imageView.frame = imageFrame;
  65. }
  66. self.titleLabel.frame = CGRectMake((CGRectGetWidth(self.bounds) - textSize.width)/2, CGRectGetHeight(self.bounds) - textSize.height, textSize.width, textSize.height);
  67. }
  68. - (CGSize)intrinsicContentSize {
  69. // 在iOS8以下,intrinsicContentSize中直接调用控件,会造成循环调用
  70. if (([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0)) {
  71. return [super intrinsicContentSize];
  72. }
  73. return [self sizeThatFits:CGSizeZero];
  74. }
  75. @end