CNavigationBarTitleButton.m 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // CNavigationBarTitleButton.m
  3. // ComPDFKit_Tools
  4. //
  5. // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. #import "CNavigationBarTitleButton.h"
  13. @implementation CNavigationBarTitleButton
  14. - (instancetype)initWithFrame:(CGRect)frame {
  15. self = [super initWithFrame:frame];
  16. if (self) {
  17. self.titleLabel.font = [UIFont systemFontOfSize:17.];
  18. [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  19. [self.titleLabel setTextAlignment:NSTextAlignmentRight];
  20. self.titleLabel.numberOfLines = 0;
  21. [self setAdjustsImageWhenDisabled:NO];
  22. [self.imageView setContentMode:UIViewContentModeCenter];
  23. }
  24. return self;
  25. }
  26. - (CGRect)imageRectForContentRect:(CGRect)contentRect {
  27. CGFloat height = contentRect.size.height;
  28. CGFloat width = height;
  29. CGFloat x = self.frame.size.width - width;
  30. CGFloat y = 0;
  31. return CGRectMake(x, y, width, height);
  32. }
  33. - (CGRect)titleRectForContentRect:(CGRect)contentRect {
  34. CGFloat height = contentRect.size.height;
  35. CGFloat width = self.frame.size.width - height;
  36. CGFloat x = 0;
  37. CGFloat y = 0;
  38. return CGRectMake(x, y, width, height);
  39. }
  40. - (void)setTitle:(NSString *)title forState:(UIControlState)state {
  41. [super setTitle:title forState:state];
  42. NSDictionary *param = @{NSFontAttributeName:self.titleLabel.font};
  43. CGFloat titleWidth = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, self.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:param context:nil].size.width;
  44. CGRect frame = self.frame;
  45. frame.size.width = titleWidth + self.frame.size.height + 10;
  46. self.frame = frame;
  47. }
  48. @end