VLCPlaylistGridViewCell.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // VLCGridViewCell.m
  3. // AspenProject
  4. //
  5. // Created by Felix Paul Kühne on 11.04.13.
  6. // Copyright (c) 2013 VideoLAN. All rights reserved.
  7. //
  8. #import "VLCPlaylistGridViewCell.h"
  9. @interface VLCPlaylistGridViewCell (Hack)
  10. @property (nonatomic, retain) NSString * reuseIdentifier;
  11. @end
  12. @implementation VLCPlaylistGridViewCell
  13. - (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier
  14. {
  15. self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier];
  16. if (self == nil)
  17. return nil;
  18. _thumbnailView = [[UIImageView alloc] initWithFrame:CGRectZero];
  19. _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  20. _titleLabel.highlightedTextColor = [UIColor whiteColor];
  21. _titleLabel.textColor = [UIColor colorWithWhite:.95 alpha:1.];
  22. _titleLabel.font = [UIFont boldSystemFontOfSize:12.];
  23. _titleLabel.adjustsFontSizeToFitWidth = YES;
  24. _titleLabel.minimumFontSize = 10.;
  25. _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  26. _subtitleLabel.highlightedTextColor = [UIColor whiteColor];
  27. _subtitleLabel.textColor = [UIColor colorWithWhite:.95 alpha:1.];
  28. _subtitleLabel.font = [UIFont systemFontOfSize:9.];
  29. _subtitleLabel.adjustsFontSizeToFitWidth = YES;
  30. _subtitleLabel.minimumFontSize = 8.;
  31. self.backgroundColor = [UIColor colorWithWhite:.5 alpha:1.];
  32. self.contentView.backgroundColor = self.backgroundColor;
  33. _thumbnailView.backgroundColor = self.backgroundColor;
  34. _titleLabel.backgroundColor = self.backgroundColor;
  35. _subtitleLabel.backgroundColor = self.backgroundColor;
  36. [self.contentView addSubview:_thumbnailView];
  37. [self.contentView addSubview:_titleLabel];
  38. [self.contentView addSubview:_subtitleLabel];
  39. return self;
  40. }
  41. - (UIImage *)thumbnail
  42. {
  43. return _thumbnailView.image;
  44. }
  45. - (void)setThumbnail:(UIImage *)newThumb
  46. {
  47. _thumbnailView.image = newThumb;
  48. [self setNeedsLayout];
  49. }
  50. - (NSString *)title
  51. {
  52. return _titleLabel.text;
  53. }
  54. - (void)setTitle:(NSString *)newTitle
  55. {
  56. _titleLabel.text = newTitle;
  57. [self setNeedsLayout];
  58. }
  59. - (NSString *)subtitle
  60. {
  61. return @"";
  62. }
  63. - (void)setSubtitle:(NSString *)newSubtitle
  64. {
  65. _subtitleLabel.text = newSubtitle;
  66. [self setNeedsLayout];
  67. }
  68. - (void)layoutSubviews
  69. {
  70. [super layoutSubviews];
  71. CGSize imageSize = _thumbnailView.image.size;
  72. CGRect bounds = CGRectInset(self.contentView.bounds, 10., 10.);
  73. [_titleLabel sizeToFit];
  74. CGRect frame = _titleLabel.frame;
  75. frame.size.width = MIN(frame.size.width, bounds.size.width);
  76. frame.origin.y = CGRectGetMaxY(bounds) - frame.size.height - 15.;
  77. frame.origin.x = floorf((bounds.size.width - frame.size.width) * 0.5);
  78. _titleLabel.frame = frame;
  79. [_subtitleLabel sizeToFit];
  80. frame = _subtitleLabel.frame;
  81. frame.size.width = MIN(frame.size.width, bounds.size.width);
  82. frame.origin.y = CGRectGetMaxY(bounds) - frame.size.height;
  83. frame.origin.x = floorf((bounds.size.width - frame.size.width) * 0.5);
  84. _subtitleLabel.frame = frame;
  85. bounds.size.height = frame.origin.y - bounds.origin.y;
  86. if ((imageSize.width <= bounds.size.width) && (imageSize.height <= bounds.size.height))
  87. return;
  88. CGFloat hRatio = bounds.size.width / imageSize.width;
  89. CGFloat vRatio = bounds.size.height / imageSize.height;
  90. CGFloat ratio = MIN(hRatio, vRatio);
  91. [_thumbnailView sizeToFit];
  92. frame = _thumbnailView.frame;
  93. frame.size.width = floorf(imageSize.width * ratio);
  94. frame.size.height = floorf(imageSize.height * ratio);
  95. frame.origin.x = floorf((bounds.size.width - frame.size.width) * 0.5);
  96. frame.origin.y = floorf((bounds.size.height - frame.size.height) * 0.5);
  97. _thumbnailView.frame = frame;
  98. }
  99. @end