CaptionView.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2016-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "CaptionView.h"
  21. #import "MediaBrowserFile.h"
  22. #import "RectUtil.h"
  23. #import "Utils.h"
  24. static const CGFloat labelPadding = 10.0;
  25. static const CGFloat defaultCustomViewHeight = 24.0;
  26. @interface CaptionView ()
  27. @property BaseMessage *message;
  28. @property UIView *customView;
  29. @property UILabel *timeLabel;
  30. @end
  31. @implementation CaptionView
  32. -(id)initWithPhoto:(id<MWPhoto>)photo {
  33. self = [super initWithPhoto:photo];
  34. if (self) {
  35. if ([photo respondsToSelector:@selector(sourceReference)]) {
  36. _message = [photo performSelector:@selector(sourceReference)];
  37. CGRect customViewRect = CGRectMake(labelPadding, labelPadding, self.bounds.size.width-labelPadding*2, defaultCustomViewHeight);
  38. _customView = [self customViewInRect:customViewRect];
  39. [self addSubview:_customView];
  40. CGRect timeLabelRect = [RectUtil setYPositionOf:customViewRect y:CGRectGetMaxY(customViewRect) + labelPadding];
  41. _timeLabel = [self createLabelInRect:timeLabelRect];
  42. [_timeLabel setText: [DateFormatter shortStyleDateTime:_message.remoteSentDate]];
  43. [self addSubview:_timeLabel];
  44. // adapt to height of resulting custom view
  45. if (_customView.frame.size.height != defaultCustomViewHeight) {
  46. _timeLabel.frame = [RectUtil setYPositionOf:_timeLabel.frame y:CGRectGetMaxY(_customView.frame)];
  47. self.frame = [RectUtil setHeightOf:self.frame height:CGRectGetMaxY(_timeLabel.frame)];
  48. }
  49. }
  50. }
  51. self.userInteractionEnabled = YES;
  52. return self;
  53. }
  54. - (void)setupCaption {
  55. ;// ignore
  56. }
  57. -(CGSize)sizeThatFits:(CGSize)size {
  58. CGSize labelSize = CGSizeMake(size.width, CGRectGetMaxY(_timeLabel.frame) + labelPadding);
  59. return labelSize;
  60. }
  61. - (UIView *)customViewInRect:(CGRect)rect {
  62. return nil;
  63. }
  64. - (UILabel *)createLabelInRect:(CGRect)rect {
  65. UILabel *label = [[UILabel alloc] initWithFrame:rect];
  66. label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  67. label.opaque = NO;
  68. label.backgroundColor = [UIColor clearColor];
  69. label.textAlignment = NSTextAlignmentCenter;
  70. label.lineBreakMode = NSLineBreakByWordWrapping;
  71. label.numberOfLines = 1;
  72. label.textColor = [UIColor whiteColor];
  73. label.font = [UIFont systemFontOfSize:17];
  74. return label;
  75. }
  76. - (UITextView *)createTextViewInRect:(CGRect)rect {
  77. UITextView *textView = [[UITextView alloc] initWithFrame:rect];
  78. textView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  79. textView.opaque = NO;
  80. textView.backgroundColor = [UIColor clearColor];
  81. textView.textAlignment = NSTextAlignmentCenter;
  82. textView.textColor = [UIColor whiteColor];
  83. textView.font = [UIFont systemFontOfSize:17.0];
  84. return textView;
  85. }
  86. @end