MWCaptionView.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. //TWS Private
  13. /*
  14. @interface MWCaptionView () {
  15. id <MWPhoto> _photo;
  16. UILabel *_label;
  17. }
  18. @end
  19. */
  20. @implementation MWCaptionView
  21. - (id)initWithPhoto:(id<MWPhoto>)photo {
  22. self = [super initWithFrame:CGRectMake(0, 0, 320, 44)]; // Random initial frame
  23. if (self) {
  24. self.userInteractionEnabled = NO;
  25. _photo = photo;
  26. self.barStyle = UIBarStyleDefault; //TWS
  27. self.tintColor = nil;
  28. self.barTintColor = nil;
  29. self.barStyle = UIBarStyleDefault; //TWS
  30. [self setBackgroundImage:nil forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
  31. self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  32. [self setupCaption];
  33. }
  34. return self;
  35. }
  36. - (CGSize)sizeThatFits:(CGSize)size {
  37. CGFloat maxHeight = 9999;
  38. if (_label.numberOfLines > 0) maxHeight = _label.font.leading*_label.numberOfLines;
  39. //CGSize textSize = [_label.text boundingRectWithSize:CGSizeMake(size.width - labelPadding*2, maxHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:_label.font} context:nil].size;
  40. //return CGSizeMake(size.width, textSize.height + labelPadding * 2);
  41. //TWS
  42. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) return CGSizeMake(size.width, 50);
  43. else return CGSizeMake(size.width, 49);
  44. }
  45. - (void)setupCaption {
  46. _label = [[UILabel alloc] initWithFrame:CGRectIntegral(CGRectMake(labelPadding, 0,
  47. self.bounds.size.width-labelPadding*2,
  48. self.bounds.size.height))];
  49. _label.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  50. _label.opaque = NO;
  51. _label.backgroundColor = [UIColor clearColor];
  52. _label.textAlignment = NSTextAlignmentLeft;
  53. _label.lineBreakMode = NSLineBreakByWordWrapping;
  54. _label.numberOfLines = 0;
  55. _label.textColor = [UIColor blackColor];
  56. _label.font = [UIFont systemFontOfSize:12];
  57. if ([_photo respondsToSelector:@selector(caption)]) {
  58. _label.text = [_photo caption] ? [_photo caption] : @" ";
  59. }
  60. [self addSubview:_label];
  61. }
  62. @end