// // Copyright (c) 2018 Open Whisper Systems. All rights reserved. // #import "OWSGenericAttachmentView.h" #import "OWSBezierPathView.h" #import "UIFont+OWS.h" #import "UIView+OWS.h" #import "ViewControllerUtils.h" #import #import #import #import #import NS_ASSUME_NONNULL_BEGIN @interface OWSGenericAttachmentView () @property (nonatomic) TSAttachmentStream *attachmentStream; @property (nonatomic) BOOL isIncoming; @property (nonatomic) UILabel *topLabel; @property (nonatomic) UILabel *bottomLabel; @end #pragma mark - @implementation OWSGenericAttachmentView - (instancetype)initWithAttachment:(TSAttachmentStream *)attachmentStream isIncoming:(BOOL)isIncoming { self = [super init]; if (self) { _attachmentStream = attachmentStream; _isIncoming = isIncoming; } return self; } #pragma mark - - (CGFloat)hMargin { return 0.f; } - (CGFloat)hSpacing { return 8.f; } - (CGFloat)vMargin { return 0.f; } - (CGSize)measureSizeWithMaxMessageWidth:(CGFloat)maxMessageWidth { CGSize result = CGSizeZero; CGFloat labelsHeight = ([OWSGenericAttachmentView topLabelFont].lineHeight + [OWSGenericAttachmentView bottomLabelFont].lineHeight + [OWSGenericAttachmentView labelVSpacing]); CGFloat contentHeight = MAX(self.iconHeight, labelsHeight); result.height = contentHeight + self.vMargin * 2; CGFloat labelsWidth = MAX([self.topLabel sizeThatFits:CGSizeZero].width, [self.bottomLabel sizeThatFits:CGSizeZero].width); CGFloat contentWidth = (self.iconWidth + labelsWidth + self.hSpacing); result.width = MIN(maxMessageWidth, contentWidth + self.hMargin * 2); return CGSizeCeil(result); } - (CGFloat)iconWidth { return 36.f; } - (CGFloat)iconHeight { return 48.f; } - (void)createContents { self.axis = UILayoutConstraintAxisHorizontal; self.alignment = UIStackViewAlignmentCenter; self.spacing = self.hSpacing; // attachment_file UIImage *image = [UIImage imageNamed:@"generic-attachment"]; OWSAssert(image); OWSAssert(image.size.width == self.iconWidth); OWSAssert(image.size.height == self.iconHeight); UIImageView *imageView = [UIImageView new]; imageView.image = image; [self addArrangedSubview:imageView]; [imageView setContentHuggingHigh]; NSString *filename = self.attachmentStream.sourceFilename; if (!filename) { filename = [[self.attachmentStream filePath] lastPathComponent]; } NSString *fileExtension = filename.pathExtension; if (fileExtension.length < 1) { fileExtension = [MIMETypeUtil fileExtensionForMIMEType:self.attachmentStream.contentType]; } UILabel *fileTypeLabel = [UILabel new]; fileTypeLabel.text = fileExtension.uppercaseString; fileTypeLabel.textColor = [UIColor ows_light90Color]; fileTypeLabel.lineBreakMode = NSLineBreakByTruncatingTail; fileTypeLabel.font = [UIFont ows_dynamicTypeCaption1Font].ows_mediumWeight; fileTypeLabel.adjustsFontSizeToFitWidth = YES; fileTypeLabel.textAlignment = NSTextAlignmentCenter; // Center on icon. [imageView addSubview:fileTypeLabel]; [fileTypeLabel autoCenterInSuperview]; [fileTypeLabel autoSetDimension:ALDimensionWidth toSize:self.iconWidth - 20.f]; UIStackView *labelsView = [UIStackView new]; labelsView.axis = UILayoutConstraintAxisVertical; labelsView.spacing = [OWSGenericAttachmentView labelVSpacing]; labelsView.alignment = UIStackViewAlignmentLeading; [self addArrangedSubview:labelsView]; NSString *topText = [self.attachmentStream.sourceFilename ows_stripped]; if (topText.length < 1) { topText = [MIMETypeUtil fileExtensionForMIMEType:self.attachmentStream.contentType].uppercaseString; } if (topText.length < 1) { topText = NSLocalizedString(@"GENERIC_ATTACHMENT_LABEL", @"A label for generic attachments."); } UILabel *topLabel = [UILabel new]; self.topLabel = topLabel; topLabel.text = topText; topLabel.textColor = (self.isIncoming ? [UIColor ows_whiteColor] : [UIColor ows_light90Color]); topLabel.lineBreakMode = NSLineBreakByTruncatingMiddle; topLabel.font = [OWSGenericAttachmentView topLabelFont]; [labelsView addArrangedSubview:topLabel]; NSError *error; unsigned long long fileSize = [[NSFileManager defaultManager] attributesOfItemAtPath:[self.attachmentStream filePath] error:&error].fileSize; OWSAssert(!error); NSString *bottomText = [OWSFormat formatFileSize:fileSize]; UILabel *bottomLabel = [UILabel new]; self.bottomLabel = bottomLabel; bottomLabel.text = bottomText; bottomLabel.textColor = (self.isIncoming ? [UIColor colorWithWhite:1.f alpha:0.7f] : [UIColor ows_light60Color]); bottomLabel.lineBreakMode = NSLineBreakByTruncatingMiddle; bottomLabel.font = [OWSGenericAttachmentView bottomLabelFont]; [labelsView addArrangedSubview:bottomLabel]; } + (UIFont *)topLabelFont { return [UIFont ows_dynamicTypeCaption1Font]; } + (UIFont *)bottomLabelFont { return [UIFont ows_dynamicTypeCaption2Font]; } + (CGFloat)labelVSpacing { return 2.f; } @end NS_ASSUME_NONNULL_END