MWCaptionView.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // MWCaptionView.m
  3. // MWPhotoBrowser
  4. //
  5. // Created by Michael Waterfall on 30/12/2011.
  6. // Copyright (c) 2011 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "MWCommon.h"
  9. #import "MWCaptionView.h"
  10. #import "MWPhoto.h"
  11. static const CGFloat labelPadding = 10;
  12. // Private
  13. @interface MWCaptionView () {
  14. id <MWPhoto> _photo;
  15. UILabel *_label;
  16. }
  17. @end
  18. @implementation MWCaptionView
  19. - (id)initWithPhoto:(id<MWPhoto>)photo {
  20. self = [super initWithFrame:CGRectMake(0, 0, 320, 44)]; // Random initial frame
  21. if (self) {
  22. self.userInteractionEnabled = NO;
  23. _photo = photo;
  24. if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7")) {
  25. // Use iOS 7 blurry goodness
  26. self.barStyle = UIBarStyleBlackTranslucent;
  27. self.tintColor = nil;
  28. self.barTintColor = nil;
  29. self.barStyle = UIBarStyleBlackTranslucent;
  30. [self setBackgroundImage:nil forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
  31. } else {
  32. // Transparent black with no gloss
  33. CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
  34. UIGraphicsBeginImageContext(rect.size);
  35. CGContextRef context = UIGraphicsGetCurrentContext();
  36. CGContextSetFillColorWithColor(context, [[UIColor colorWithWhite:0 alpha:0.6] CGColor]);
  37. CGContextFillRect(context, rect);
  38. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  39. UIGraphicsEndImageContext();
  40. [self setBackgroundImage:image forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
  41. }
  42. self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  43. [self setupCaption];
  44. }
  45. return self;
  46. }
  47. - (CGSize)sizeThatFits:(CGSize)size {
  48. CGFloat maxHeight = 9999;
  49. if (_label.numberOfLines > 0) maxHeight = _label.font.leading*_label.numberOfLines;
  50. CGSize textSize;
  51. if ([NSString instancesRespondToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
  52. textSize = [_label.text boundingRectWithSize:CGSizeMake(size.width - labelPadding*2, maxHeight)
  53. options:NSStringDrawingUsesLineFragmentOrigin
  54. attributes:@{NSFontAttributeName:_label.font}
  55. context:nil].size;
  56. } else {
  57. #pragma clang diagnostic push
  58. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  59. textSize = [_label.text sizeWithFont:_label.font
  60. constrainedToSize:CGSizeMake(size.width - labelPadding*2, maxHeight)
  61. lineBreakMode:_label.lineBreakMode];
  62. #pragma clang diagnostic pop
  63. }
  64. return CGSizeMake(size.width, textSize.height + labelPadding * 2);
  65. }
  66. - (void)setupCaption {
  67. _label = [[UILabel alloc] initWithFrame:CGRectIntegral(CGRectMake(labelPadding, 0,
  68. self.bounds.size.width-labelPadding*2,
  69. self.bounds.size.height))];
  70. _label.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  71. _label.opaque = NO;
  72. _label.backgroundColor = [UIColor clearColor];
  73. if (SYSTEM_VERSION_LESS_THAN(@"6")) {
  74. #pragma clang diagnostic push
  75. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  76. _label.textAlignment = UITextAlignmentCenter;
  77. _label.lineBreakMode = UILineBreakModeWordWrap;
  78. #pragma clang diagnostic pop
  79. } else {
  80. _label.textAlignment = NSTextAlignmentCenter;
  81. _label.lineBreakMode = NSLineBreakByWordWrapping;
  82. }
  83. _label.numberOfLines = 0;
  84. _label.textColor = [UIColor whiteColor];
  85. if (SYSTEM_VERSION_LESS_THAN(@"7")) {
  86. // Shadow on 6 and below
  87. _label.shadowColor = [UIColor blackColor];
  88. _label.shadowOffset = CGSizeMake(1, 1);
  89. }
  90. _label.font = [UIFont systemFontOfSize:17];
  91. if ([_photo respondsToSelector:@selector(caption)]) {
  92. _label.text = [_photo caption] ? [_photo caption] : @" ";
  93. }
  94. [self addSubview:_label];
  95. }
  96. @end