VLCStatusLabel.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*****************************************************************************
  2. * VLCStatusLabel.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Gleb Pinigin <gpinigin # gmail.com>
  10. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. #import "VLCStatusLabel.h"
  14. @interface VLCStatusLabel ()
  15. {
  16. NSTimer *_displayTimer;
  17. }
  18. @end
  19. @implementation VLCStatusLabel
  20. - (instancetype)initWithFrame:(CGRect)frame
  21. {
  22. self = [super initWithFrame:frame];
  23. if (self)
  24. [self initialize];
  25. return self;
  26. }
  27. - (instancetype)initWithCoder:(NSCoder *)aDecoder
  28. {
  29. self = [super initWithCoder:aDecoder];
  30. if (self)
  31. [self initialize];
  32. return self;
  33. }
  34. - (void)initialize
  35. {
  36. self.backgroundColor = [UIColor clearColor];
  37. self.textAlignment = NSTextAlignmentCenter;
  38. }
  39. #pragma mark -
  40. - (void)showStatusMessage:(NSString *)message
  41. {
  42. self.text = message;
  43. /* layout and horizontal center in super view */
  44. [self sizeToFit];
  45. CGRect selfFrame = self.frame;
  46. CGRect parentFrame = [self superview].bounds;
  47. selfFrame.origin.x = (parentFrame.size.width - selfFrame.size.width) / 2.;
  48. [self setFrame:selfFrame];
  49. [self setNeedsDisplay];
  50. if (_displayTimer)
  51. [_displayTimer invalidate];
  52. else
  53. [self setHidden:NO animated:YES];
  54. _displayTimer = [NSTimer scheduledTimerWithTimeInterval:1.5
  55. target:self
  56. selector:@selector(_hideAgain)
  57. userInfo:nil
  58. repeats:NO];
  59. }
  60. - (void)_hideAgain
  61. {
  62. [self setHidden:YES animated:YES];
  63. _displayTimer = nil;
  64. }
  65. - (void)setHidden:(BOOL)hidden animated:(BOOL)animated
  66. {
  67. CGFloat alpha = hidden? 0.0f: 1.0f;
  68. if (!hidden) {
  69. self.alpha = 0.0f;
  70. self.hidden = NO;
  71. }
  72. void (^animationBlock)() = ^() {
  73. self.alpha = alpha;
  74. };
  75. void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
  76. self.hidden = hidden;
  77. };
  78. NSTimeInterval duration = animated? 0.3: 0.0;
  79. [UIView animateWithDuration:duration animations:animationBlock completion:completionBlock];
  80. }
  81. #pragma mark - sizing
  82. - (CGSize)sizeThatFits:(CGSize)size
  83. {
  84. CGSize textSize = [self.text sizeWithFont:self.font];
  85. textSize.width += 16.f; // take extra width into account for our custom drawing
  86. return textSize;
  87. }
  88. #pragma mark -
  89. - (void)drawRect:(CGRect)rect
  90. {
  91. UIColor *drawingColor = [UIColor VLCDarkBackgroundColor];
  92. [drawingColor setFill];
  93. UIBezierPath* bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:rect.size.height / 2];
  94. [bezierPath fill];
  95. [super drawRect:rect];
  96. }
  97. @end