PhotoCaptionView.m 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 "PhotoCaptionView.h"
  21. #import "ImageMessage.h"
  22. #import "RectUtil.h"
  23. #import "TextStyleUtils.h"
  24. #define PADDING 10.0
  25. @interface PhotoCaptionView ()
  26. @property UITextView *textView;
  27. @end
  28. @implementation PhotoCaptionView
  29. - (UIView *)customViewInRect:(CGRect)rect {
  30. ImageMessage *imageMessage = (ImageMessage*)self.message;
  31. NSString *caption = [imageMessage.image getCaption];
  32. if (caption) {
  33. _textView = [self createTextViewInRect:rect];
  34. _textView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  35. _textView.textAlignment = NSTextAlignmentCenter;
  36. _textView.editable = NO;
  37. _textView.userInteractionEnabled = YES;
  38. _textView.scrollEnabled = YES;
  39. _textView.backgroundColor = [UIColor clearColor];
  40. _textView.selectable = NO;
  41. _textView.text = [TextStyleUtils makeMentionsStringForText:caption];
  42. CGFloat maxHeight = 200.0;
  43. CGSize textSize = [_textView.text boundingRectWithSize:CGSizeMake(self.frame.size.width, maxHeight)
  44. options:NSStringDrawingUsesLineFragmentOrigin
  45. attributes:@{NSFontAttributeName:_textView.font}
  46. context:nil].size;
  47. CGSize size = CGSizeMake(textSize.width, textSize.height);
  48. _textView.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y + 2.0, self.frame.size.width, size.height);
  49. return _textView;
  50. }
  51. return [[UIView alloc] initWithFrame:CGRectMake(0.0, rect.origin.y, rect.size.width, 0.0)];
  52. }
  53. @end