IDMCaptionView.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // IDMCaptionView.m
  3. // IDMPhotoBrowser
  4. //
  5. // Created by Michael Waterfall on 30/12/2011.
  6. // Copyright (c) 2011 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "IDMCaptionView.h"
  9. #import "IDMPhoto.h"
  10. #import <QuartzCore/QuartzCore.h>
  11. static const CGFloat labelPadding = 10;
  12. // Private
  13. @interface IDMCaptionView () {
  14. id<IDMPhoto> _photo;
  15. UILabel *_label;
  16. }
  17. @end
  18. @implementation IDMCaptionView
  19. - (id)initWithPhoto:(id<IDMPhoto>)photo {
  20. CGRect screenBound = [[UIScreen mainScreen] bounds];
  21. CGFloat screenWidth = screenBound.size.width;
  22. if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft ||
  23. [[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) {
  24. screenWidth = screenBound.size.height;
  25. }
  26. self = [super initWithFrame:CGRectMake(0, 0, screenWidth, 44)]; // Random initial frame
  27. if (self) {
  28. _photo = photo;
  29. self.opaque = NO;
  30. [self setBackground];
  31. [self setupCaption];
  32. }
  33. return self;
  34. }
  35. - (CGSize)sizeThatFits:(CGSize)size {
  36. if (_label.text.length == 0) return CGSizeZero;
  37. CGFloat maxHeight = 9999;
  38. if (_label.numberOfLines > 0) maxHeight = _label.font.leading*_label.numberOfLines;
  39. /*CGSize textSizeOLD = [_label.text sizeWithFont:_label.font
  40. constrainedToSize:CGSizeMake(size.width - labelPadding*2, maxHeight)
  41. lineBreakMode:_label.lineBreakMode];*/
  42. NSString *text = _label.text;
  43. CGFloat width = size.width - labelPadding*2;
  44. UIFont *font = _label.font;
  45. NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text
  46. attributes:@{NSFontAttributeName: font}];
  47. CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, maxHeight}
  48. options:NSStringDrawingUsesLineFragmentOrigin
  49. context:nil];
  50. CGSize textSize = rect.size;
  51. return CGSizeMake(size.width, textSize.height + labelPadding * 2);
  52. }
  53. - (void)setupCaption {
  54. _label = [[UILabel alloc] initWithFrame:CGRectMake(labelPadding, 0,
  55. self.bounds.size.width-labelPadding*2,
  56. self.bounds.size.height)];
  57. _label.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  58. _label.opaque = NO;
  59. _label.backgroundColor = [UIColor clearColor];
  60. _label.textAlignment = NSTextAlignmentCenter;
  61. _label.lineBreakMode = NSLineBreakByWordWrapping;
  62. _label.numberOfLines = 3;
  63. _label.textColor = [UIColor whiteColor];
  64. _label.shadowColor = [UIColor colorWithWhite:0 alpha:0.5];
  65. _label.shadowOffset = CGSizeMake(0, 1);
  66. _label.font = [UIFont systemFontOfSize:17];
  67. if ([_photo respondsToSelector:@selector(caption)]) {
  68. _label.text = [_photo caption] ? [_photo caption] : @" ";
  69. }
  70. [self addSubview:_label];
  71. }
  72. - (void)setBackground {
  73. UIView *fadeView = [[UIView alloc] initWithFrame:CGRectMake(0, -100, 10000, 130+100)]; // Static width, autoresizingMask is not working
  74. CAGradientLayer *gradient = [CAGradientLayer layer];
  75. gradient.frame = fadeView.bounds;
  76. gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:0 alpha:0.0] CGColor], (id)[[UIColor colorWithWhite:0 alpha:0.8] CGColor], nil];
  77. [fadeView.layer insertSublayer:gradient atIndex:0];
  78. fadeView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; //UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  79. [self addSubview:fadeView];
  80. }
  81. @end