FileCaptionView.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "FileCaptionView.h"
  21. #import "FileMessage.h"
  22. #import "MediaBrowserFile.h"
  23. #import "RectUtil.h"
  24. #import "Utils.h"
  25. #import "TextStyleUtils.h"
  26. #define PADDING 10.0
  27. @implementation FileCaptionView
  28. - (UIView *)customViewInRect:(CGRect)rect {
  29. FileMessage *fileMessage = (FileMessage *)self.message;
  30. UIView *view = nil;
  31. if (!([fileMessage renderMediaFileMessage] || [fileMessage renderStickerFileMessage])) {
  32. view = [[UIView alloc] initWithFrame:rect];
  33. CGFloat rightWidth = 100.0;
  34. CGRect rightRect = CGRectMake(rect.size.width - rightWidth, 0.0, rightWidth, rect.size.height);
  35. UILabel *labelRight = [self createLabelInRect:rightRect];
  36. labelRight.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  37. labelRight.lineBreakMode = NSLineBreakByTruncatingMiddle;
  38. labelRight.textAlignment = NSTextAlignmentRight;
  39. labelRight.text = [Utils formatDataLength:fileMessage.fileSize.floatValue];
  40. [view addSubview:labelRight];
  41. CGRect leftRect = CGRectMake(0.0, 0.0, rect.size.width - rightWidth, rect.size.height);
  42. UILabel *labelLeft = [self createLabelInRect:leftRect];
  43. labelLeft.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  44. labelLeft.lineBreakMode = NSLineBreakByTruncatingMiddle;
  45. labelLeft.textAlignment = NSTextAlignmentLeft;
  46. labelLeft.text = fileMessage.fileName;
  47. [view addSubview:labelLeft];
  48. }
  49. NSString *caption = [fileMessage getCaption];
  50. if (caption) {
  51. UITextView *textView = [self createTextViewInRect:rect];
  52. textView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  53. textView.textAlignment = NSTextAlignmentCenter;
  54. textView.editable = NO;
  55. textView.userInteractionEnabled = YES;
  56. textView.scrollEnabled = YES;
  57. textView.backgroundColor = [UIColor clearColor];
  58. textView.selectable = NO;
  59. textView.text = [TextStyleUtils makeMentionsStringForText:caption];
  60. CGFloat maxHeight = 200.0;
  61. CGSize textSize = [textView.text boundingRectWithSize:CGSizeMake(self.frame.size.width, maxHeight)
  62. options:NSStringDrawingUsesLineFragmentOrigin
  63. attributes:@{NSFontAttributeName:textView.font}
  64. context:nil].size;
  65. CGSize size = CGSizeMake(textSize.width, textSize.height);
  66. if (view == nil) {
  67. textView.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y + 2.0, self.frame.size.width, size.height);
  68. return textView;
  69. } else {
  70. textView.frame = CGRectMake(0, rect.size.height + 2.0 , rect.size.width, size.height);
  71. [view addSubview:textView];
  72. view.frame = [RectUtil setHeightOf:view.frame height:rect.size.height + textView.frame.size.height];
  73. }
  74. }
  75. if ([view subviews].count > 0) {
  76. view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  77. return view;
  78. }
  79. return [[UIView alloc] initWithFrame:CGRectMake(0.0, rect.origin.y, rect.size.width, 0.0)];
  80. }
  81. @end