From c91bc71ebdd36ac248f0c18c37443f49bc0b2be9 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Thu, 9 Aug 2018 10:47:20 -0400 Subject: [PATCH] Cache sender name size. --- Signal.xcodeproj/project.pbxproj | 6 ++ .../ConversationView/Cells/OWSLabel.h | 11 +++ .../ConversationView/Cells/OWSLabel.m | 76 +++++++++++++++++++ .../Cells/OWSMessageBubbleView.m | 3 +- 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 Signal/src/ViewControllers/ConversationView/Cells/OWSLabel.h create mode 100644 Signal/src/ViewControllers/ConversationView/Cells/OWSLabel.m diff --git a/Signal.xcodeproj/project.pbxproj b/Signal.xcodeproj/project.pbxproj index ed22b3fcb..50bb36752 100644 --- a/Signal.xcodeproj/project.pbxproj +++ b/Signal.xcodeproj/project.pbxproj @@ -207,6 +207,7 @@ 34AC0A1E211B39EA00997B47 /* ThreadViewHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 34AC0A0B211B39EA00997B47 /* ThreadViewHelper.m */; }; 34AC0A1F211B39EA00997B47 /* AvatarImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34AC0A0C211B39EA00997B47 /* AvatarImageView.swift */; }; 34AC0A20211B39EA00997B47 /* ThreadViewHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = 34AC0A0D211B39EA00997B47 /* ThreadViewHelper.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 34AC0A23211C829F00997B47 /* OWSLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = 34AC0A21211C829E00997B47 /* OWSLabel.m */; }; 34B0796D1FCF46B100E248C2 /* MainAppContext.m in Sources */ = {isa = PBXBuildFile; fileRef = 34B0796B1FCF46B000E248C2 /* MainAppContext.m */; }; 34B3F8751E8DF1700035BE1A /* CallViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34B3F83B1E8DF1700035BE1A /* CallViewController.swift */; }; 34B3F8771E8DF1700035BE1A /* ContactsPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34B3F83E1E8DF1700035BE1A /* ContactsPicker.swift */; }; @@ -841,6 +842,8 @@ 34AC0A0B211B39EA00997B47 /* ThreadViewHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ThreadViewHelper.m; sourceTree = ""; }; 34AC0A0C211B39EA00997B47 /* AvatarImageView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AvatarImageView.swift; sourceTree = ""; }; 34AC0A0D211B39EA00997B47 /* ThreadViewHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ThreadViewHelper.h; sourceTree = ""; }; + 34AC0A21211C829E00997B47 /* OWSLabel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OWSLabel.m; sourceTree = ""; }; + 34AC0A22211C829E00997B47 /* OWSLabel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OWSLabel.h; sourceTree = ""; }; 34B0796B1FCF46B000E248C2 /* MainAppContext.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MainAppContext.m; sourceTree = ""; }; 34B0796C1FCF46B000E248C2 /* MainAppContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MainAppContext.h; sourceTree = ""; }; 34B0796E1FD07B1E00E248C2 /* SignalShareExtension.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = SignalShareExtension.entitlements; sourceTree = ""; }; @@ -1819,6 +1822,8 @@ 34CA631A2097806E00E526A0 /* OWSContactShareView.m */, 34D1F0B51F87F8850066283D /* OWSGenericAttachmentView.h */, 34D1F0B61F87F8850066283D /* OWSGenericAttachmentView.m */, + 34AC0A22211C829E00997B47 /* OWSLabel.h */, + 34AC0A21211C829E00997B47 /* OWSLabel.m */, 3496744B2076768600080B5F /* OWSMessageBubbleView.h */, 3496744C2076768700080B5F /* OWSMessageBubbleView.m */, 34D1F0A11F867BFC0066283D /* OWSMessageCell.h */, @@ -3390,6 +3395,7 @@ 457F671B20746193000EABCD /* QuotedReplyPreview.swift in Sources */, 34DBF004206BD5A500025978 /* OWSBubbleView.m in Sources */, 34E88D262098C5AE00A608F4 /* ContactViewController.swift in Sources */, + 34AC0A23211C829F00997B47 /* OWSLabel.m in Sources */, 76EB054018170B33006006FC /* AppDelegate.m in Sources */, 34D1F0831F8678AA0066283D /* ConversationInputTextView.m in Sources */, 340FC8B6204DAC8D007AEB0F /* OWSQRCodeScanningViewController.m in Sources */, diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSLabel.h b/Signal/src/ViewControllers/ConversationView/Cells/OWSLabel.h new file mode 100644 index 000000000..89992ef6f --- /dev/null +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSLabel.h @@ -0,0 +1,11 @@ +// +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. +// + +NS_ASSUME_NONNULL_BEGIN + +@interface OWSLabel : UILabel + +@end + +NS_ASSUME_NONNULL_END diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSLabel.m b/Signal/src/ViewControllers/ConversationView/Cells/OWSLabel.m new file mode 100644 index 000000000..f45699922 --- /dev/null +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSLabel.m @@ -0,0 +1,76 @@ +// +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. +// + +#import "OWSLabel.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface OWSLabel () + +@property (nonatomic, nullable) NSValue *cachedSize; + +@end + +#pragma mark - + +@implementation OWSLabel + +- (void)setText:(nullable NSString *)text +{ + if ([NSObject isNullableObject:text equalTo:self.text]) { + return; + } + [super setText:text]; + self.cachedSize = nil; +} + +- (void)setAttributedText:(nullable NSAttributedString *)attributedText +{ + if ([NSObject isNullableObject:attributedText equalTo:self.attributedText]) { + return; + } + [super setAttributedText:attributedText]; + self.cachedSize = nil; +} + +- (void)setTextColor:(nullable UIColor *)textColor +{ + if ([NSObject isNullableObject:textColor equalTo:self.textColor]) { + return; + } + [super setTextColor:textColor]; + // No need to clear cached size here. +} + +- (void)setFont:(nullable UIFont *)font +{ + if ([NSObject isNullableObject:font equalTo:self.font]) { + return; + } + [super setFont:font]; + self.cachedSize = nil; +} + +- (void)setLineBreakMode:(NSLineBreakMode)lineBreakMode +{ + if (self.lineBreakMode == lineBreakMode) { + return; + } + [super setLineBreakMode:lineBreakMode]; + self.cachedSize = nil; +} + +- (CGSize)sizeThatFits:(CGSize)size +{ + if (self.cachedSize) { + return self.cachedSize.CGSizeValue; + } + CGSize result = [super sizeThatFits:size]; + self.cachedSize = [NSValue valueWithCGSize:result]; + return result; +} + +@end + +NS_ASSUME_NONNULL_END diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.m b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.m index 7d5292917..32572d33a 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.m +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.m @@ -11,6 +11,7 @@ #import "OWSContactShareButtonsView.h" #import "OWSContactShareView.h" #import "OWSGenericAttachmentView.h" +#import "OWSLabel.h" #import "OWSMessageFooterView.h" #import "OWSMessageTextView.h" #import "OWSQuotedMessageView.h" @@ -85,7 +86,7 @@ NS_ASSUME_NONNULL_BEGIN self.stackView = [UIStackView new]; self.stackView.axis = UILayoutConstraintAxisVertical; - self.senderNameLabel = [UILabel new]; + self.senderNameLabel = [OWSLabel new]; self.senderNameContainer = [UIView new]; self.senderNameContainer.layoutMargins = UIEdgeInsetsMake(0, 0, self.senderNameBottomSpacing, 0); [self.senderNameContainer addSubview:self.senderNameLabel];