UGCKitVerticalButton.m 2.9 KB

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