PDFKTFontNamePopBgView.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // PDFKTFontNamePopBgView.m
  3. // PDFReader
  4. //
  5. // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // The PDF Reader Sample applications are licensed with a modified BSD license.
  8. // Please see License for details. This notice may not be removed from this file.
  9. //
  10. #import "PDFKTFontNamePopBgView.h"
  11. #define CONTENT_INSET 3.0
  12. #define CAP_INSET 20.0
  13. #define ARROW_BASE 42.0
  14. #define ARROW_HEIGHT 16.0
  15. @interface PDFKTFontNamePopBgView ()
  16. {
  17. UIImageView *_borderImageView;
  18. UIImageView *_arrowView;
  19. CGFloat _arrowOffset;
  20. UIPopoverArrowDirection _arrowDirection;
  21. }
  22. @end
  23. @implementation PDFKTFontNamePopBgView
  24. - (instancetype)initWithFrame:(CGRect)frame
  25. {
  26. self = [super initWithFrame:frame];
  27. if (self) {
  28. self.backgroundColor = [UIColor clearColor];
  29. self.layer.shadowOffset = CGSizeZero;
  30. self.layer.shadowColor = [[UIColor clearColor] CGColor];
  31. self.layer.cornerRadius = 0.0;
  32. _borderImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"text_bg"]];
  33. _arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"text_arrow_bg"]];
  34. [self addSubview:_borderImageView];
  35. [self addSubview:_arrowView];
  36. }
  37. return self;
  38. }
  39. - (CGFloat) arrowOffset
  40. {
  41. return _arrowOffset;
  42. }
  43. - (void) setArrowOffset:(CGFloat)arrowOffset
  44. {
  45. _arrowOffset = arrowOffset;
  46. }
  47. - (UIPopoverArrowDirection)arrowDirection
  48. {
  49. return _arrowDirection;
  50. }
  51. - (void)setArrowDirection:(UIPopoverArrowDirection)arrowDirection
  52. {
  53. _arrowDirection = arrowDirection;
  54. }
  55. +(UIEdgeInsets)contentViewInsets
  56. {
  57. return UIEdgeInsetsMake(CONTENT_INSET, CONTENT_INSET, CONTENT_INSET, CONTENT_INSET);
  58. }
  59. +(CGFloat)arrowHeight
  60. {
  61. return ARROW_HEIGHT;
  62. }
  63. +(CGFloat)arrowBase
  64. {
  65. return ARROW_BASE;
  66. }
  67. + (BOOL)wantsDefaultContentAppearance
  68. {
  69. return NO;
  70. }
  71. - (void)layoutSubviews {
  72. [super layoutSubviews];
  73. CGFloat _height = self.frame.size.height;
  74. CGFloat _width = self.frame.size.width;
  75. CGFloat _left = 0.0;
  76. CGFloat _top = 0.0;
  77. CGFloat _coordinate = 0.0;
  78. CGAffineTransform _rotation = CGAffineTransformIdentity;
  79. switch (self.arrowDirection) {
  80. case UIPopoverArrowDirectionUp:
  81. _top += ARROW_HEIGHT;
  82. _height -= ARROW_HEIGHT;
  83. _coordinate = ((self.frame.size.width / 2) + self.arrowOffset) - (ARROW_BASE/2);
  84. _arrowView.frame = CGRectMake(_coordinate, 0, ARROW_BASE, ARROW_HEIGHT);
  85. _rotation = CGAffineTransformMakeRotation( M_PI );
  86. break;
  87. case UIPopoverArrowDirectionDown:
  88. _height -= ARROW_HEIGHT;
  89. _coordinate = ((self.frame.size.width / 2) + self.arrowOffset) - (ARROW_BASE/2);
  90. _arrowView.frame = CGRectMake(_coordinate, _height, ARROW_BASE, ARROW_HEIGHT);
  91. break;
  92. case UIPopoverArrowDirectionLeft:
  93. _left += ARROW_BASE;
  94. _width -= ARROW_BASE;
  95. _coordinate = ((self.frame.size.height / 2) + self.arrowOffset) - (ARROW_HEIGHT/2);
  96. _arrowView.frame = CGRectMake(0, _coordinate, ARROW_BASE, ARROW_HEIGHT);
  97. _rotation = CGAffineTransformMakeRotation( M_PI_2 );
  98. break;
  99. case UIPopoverArrowDirectionRight:
  100. _width -= ARROW_BASE;
  101. _coordinate = ((self.frame.size.height / 2) + self.arrowOffset)- (ARROW_HEIGHT/2);
  102. _arrowView.frame = CGRectMake(_width, _coordinate, ARROW_BASE, ARROW_HEIGHT);
  103. _rotation = CGAffineTransformMakeRotation( -M_PI_2 );
  104. break;
  105. }
  106. _borderImageView.frame = CGRectMake(_left, _top, _width, _height);
  107. [_arrowView setTransform:_rotation];
  108. }
  109. - (void)dealloc
  110. {
  111. [_borderImageView release];
  112. [_arrowView release];
  113. [super dealloc];
  114. }
  115. @end