PDFTitleView.m 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // PDFTitleView.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 "PDFTitleView.h"
  11. @interface PDFTitleView ()
  12. @property (nonatomic,retain) UILabel *titleLabel;
  13. @end
  14. @implementation PDFTitleView
  15. #pragma mark - Init Methods
  16. - (instancetype)initWithTitle:(NSString *)title {
  17. self = [super init];
  18. if (self){
  19. _titleLabel = ({
  20. UILabel *label = [UILabel new];
  21. label.text = title;
  22. label.numberOfLines = 2;
  23. label.lineBreakMode = NSLineBreakByTruncatingMiddle;
  24. label.textAlignment = NSTextAlignmentCenter;
  25. label.textColor = [UIColor whiteColor];
  26. [self addSubview:label];
  27. label;
  28. });
  29. self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.6];
  30. self.layer.cornerRadius = 3;
  31. self.clipsToBounds = YES;
  32. }
  33. return self;
  34. }
  35. - (void)dealloc {
  36. [_titleLabel release];
  37. [super dealloc];
  38. }
  39. #pragma mark - Public Methods
  40. - (void)showInView:(UIView *)view hideAfter:(NSTimeInterval)delay complete:(void (^)())completeHandle {
  41. if (self.superview) {
  42. if ([self.superview isEqual:view]) {
  43. return;
  44. } else {
  45. [self removeFromSuperview];
  46. }
  47. }
  48. CGSize size = [self.titleLabel sizeThatFits:CGSizeMake(view.frame.size.width-54, MAXFLOAT)];
  49. self.frame = CGRectMake((view.frame.size.width-(size.width+10))/2.0,
  50. 70, size.width+10, size.height+10);
  51. self.alpha = 1.0;
  52. self.titleLabel.frame = CGRectMake(5, 5, self.frame.size.width-10, self.frame.size.height-10);
  53. self.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  54. [view addSubview:self];
  55. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  56. [UIView animateWithDuration:0.3 animations:^{
  57. self.alpha = 0.0;
  58. } completion:^(BOOL finished) {
  59. if (completeHandle) {
  60. completeHandle();
  61. }
  62. [self removeFromSuperview];
  63. }];
  64. });
  65. }
  66. @end