AirplayActiveView.m 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Copyright (C) 2012 IGN Entertainment, Inc. */
  2. #import "AirplayActiveView.h"
  3. #import <QuartzCore/QuartzCore.h>
  4. @interface AirplayActiveView ()
  5. @property (readwrite, strong) CAGradientLayer *gradientLayer;
  6. @property (readwrite, strong) UIImageView *displayImageView;
  7. @property (readwrite, strong) UILabel *titleLabel;
  8. @property (readwrite, strong) UILabel *descriptionLabel;
  9. @end
  10. @implementation AirplayActiveView
  11. - (id)initWithFrame:(CGRect)frame
  12. {
  13. if ((self = [super initWithFrame:frame])) {
  14. _gradientLayer = [[CAGradientLayer alloc] init];
  15. [_gradientLayer setColors:@[
  16. (id)[[UIColor colorWithWhite:0.22f alpha:1.0f] CGColor],
  17. (id)[[UIColor colorWithWhite:0.09f alpha:1.0f] CGColor],
  18. ]];
  19. [_gradientLayer setLocations:@[ @0.0, @1.0 ]];
  20. [[self layer] addSublayer:_gradientLayer];
  21. _displayImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"airplay-display.png"]];
  22. [self addSubview:_displayImageView];
  23. _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  24. [_titleLabel setText:@"Airplay"];
  25. [_titleLabel setFont:[UIFont fontWithName:@"DINRoundCompPro" size:20.0f]];
  26. [_titleLabel setTextColor:[UIColor colorWithWhite:0.5f alpha:1.0f]];
  27. [_titleLabel setBackgroundColor:[UIColor clearColor]];
  28. [self addSubview:_titleLabel];
  29. _descriptionLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  30. [_descriptionLabel setText:@"This video is playing elsewhere"];
  31. [_descriptionLabel setFont:[UIFont fontWithName:@"DINRoundCompPro" size:14.0f]];
  32. [_descriptionLabel setTextColor:[UIColor colorWithWhite:0.36f alpha:1.0f]];
  33. [_descriptionLabel setBackgroundColor:[UIColor clearColor]];
  34. [self addSubview:_descriptionLabel];
  35. }
  36. return self;
  37. }
  38. - (void)layoutSubviews
  39. {
  40. [super layoutSubviews];
  41. CGRect bounds = [self bounds];
  42. [_gradientLayer setFrame:bounds];
  43. CGSize displayImageSize = [[_displayImageView image] size];
  44. if (bounds.size.height < 300) {
  45. displayImageSize = CGSizeMake(displayImageSize.width / 2, displayImageSize.height / 2);
  46. }
  47. CGSize titleLabelSize;
  48. CGSize descriptionLabelSize;
  49. if (@available(iOS 7.0, *)) {
  50. titleLabelSize = [[_titleLabel text] sizeWithAttributes:@{NSFontAttributeName:[_titleLabel font]}];
  51. descriptionLabelSize = [[_descriptionLabel text] sizeWithAttributes:@{NSFontAttributeName:[_descriptionLabel font]}];
  52. } else {
  53. titleLabelSize = [[_titleLabel text] sizeWithFont:[_titleLabel font]];
  54. descriptionLabelSize = [[_descriptionLabel text] sizeWithFont:[_descriptionLabel font]];
  55. }
  56. CGFloat contentHeight = displayImageSize.height + titleLabelSize.height + descriptionLabelSize.height;
  57. CGFloat y = (bounds.size.height / 2) - (contentHeight / 2);
  58. [_displayImageView setFrame:CGRectMake((bounds.size.width / 2) - (displayImageSize.width / 2),
  59. y,
  60. displayImageSize.width,
  61. displayImageSize.height)];
  62. y += displayImageSize.height;
  63. [_titleLabel setFrame:CGRectMake((bounds.size.width / 2) - (titleLabelSize.width / 2),
  64. y,
  65. titleLabelSize.width,
  66. titleLabelSize.height)];
  67. y += titleLabelSize.height - 8;
  68. [_descriptionLabel setFrame:CGRectMake((bounds.size.width / 2) - (descriptionLabelSize.width / 2),
  69. y,
  70. descriptionLabelSize.width,
  71. descriptionLabelSize.height)];
  72. }
  73. @end