From 572fee6173b166fc6f90a79e0183196647221ad5 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 27 Jun 2018 09:19:51 -0400 Subject: [PATCH] Respond to CR. --- .../Cells/OWSMessageBubbleView.m | 37 +--------------- .../Cells/OWSMessageTextView.h | 2 + .../Cells/OWSMessageTextView.m | 42 +++++++++++++++++++ 3 files changed, 46 insertions(+), 35 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.m b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.m index 2949ffc23..06bfb428f 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.m +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.m @@ -854,41 +854,8 @@ NS_ASSUME_NONNULL_BEGIN const int maxTextWidth = (int)floor(self.conversationStyle.maxMessageWidth - hMargins); OWSMessageTextView *bodyTextView = [self configureBodyTextView]; - CGSize textSize = CGSizeCeil([bodyTextView sizeThatFits:CGSizeMake(maxTextWidth, CGFLOAT_MAX)]); - - // "Compact" layout to reduce "widows", - // e.g. last lines with only a single word. - // - // After measuring the size of the text, we try to find smaller widths - // in which the text will fit without adding any height, by wrapping - // more text onto the last line. We use a binary search. - if (textSize.width > 0 && textSize.height > 0) { - NSUInteger upperBound = (NSUInteger)textSize.width; - NSUInteger lowerBound = 1; - // The more iterations we perform in our binary search, - // the more accurate the result, but the more expensive - // layout becomes. - const int kMaxIterations = 5; - for (int i = 0; i < kMaxIterations; i++) { - NSUInteger resizeWidth = (upperBound + lowerBound) / 2; - if (resizeWidth >= upperBound || resizeWidth <= lowerBound) { - break; - } - CGSize resizeSize = CGSizeCeil([bodyTextView sizeThatFits:CGSizeMake(resizeWidth, CGFLOAT_MAX)]); - BOOL success - = (resizeSize.width > 0 && resizeSize.width <= resizeWidth && resizeSize.height <= textSize.height); - if (success) { - // Success. - textSize = resizeSize; - upperBound = (NSUInteger)textSize.width; - } else { - // Failure. - lowerBound = resizeWidth; - } - } - } - textSize.width = MIN(textSize.width, maxTextWidth); - CGSize result = textSize; + const int kMaxIterations = 5; + CGSize result = [bodyTextView compactSizeThatFitsMaxWidth:maxTextWidth maxIterations:kMaxIterations]; if (includeMargins) { result.width += hMargins; diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageTextView.h b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageTextView.h index 6b7294f8c..49b856d93 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageTextView.h +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageTextView.h @@ -8,6 +8,8 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic) BOOL shouldIgnoreEvents; +- (CGSize)compactSizeThatFitsMaxWidth:(CGFloat)maxWidth maxIterations:(NSUInteger)maxIterations; + @end NS_ASSUME_NONNULL_END diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageTextView.m b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageTextView.m index f172de9d5..3a722fdd7 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageTextView.m +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageTextView.m @@ -3,6 +3,7 @@ // #import "OWSMessageTextView.h" +#import NS_ASSUME_NONNULL_BEGIN @@ -60,6 +61,47 @@ NS_ASSUME_NONNULL_BEGIN return result; } +// TODO: Add unit test. +- (CGSize)compactSizeThatFitsMaxWidth:(CGFloat)maxWidth maxIterations:(NSUInteger)maxIterations +{ + OWSAssert(maxWidth > 0); + + CGSize textSize = CGSizeCeil([self sizeThatFits:CGSizeMake(maxWidth, CGFLOAT_MAX)]); + + // "Compact" layout to reduce "widows", + // e.g. last lines with only a single word. + // + // After measuring the size of the text, we try to find smaller widths + // in which the text will fit without adding any height, by wrapping + // more text onto the last line. We use a binary search. + if (textSize.width > 0 && textSize.height > 0) { + NSUInteger upperBound = (NSUInteger)textSize.width; + NSUInteger lowerBound = 1; + // The more iterations we perform in our binary search, + // the more accurate the result, but the more expensive + // layout becomes. + for (NSUInteger i = 0; i < maxIterations; i++) { + NSUInteger resizeWidth = (upperBound + lowerBound) / 2; + if (resizeWidth >= upperBound || resizeWidth <= lowerBound) { + break; + } + CGSize resizeSize = CGSizeCeil([self sizeThatFits:CGSizeMake(resizeWidth, CGFLOAT_MAX)]); + BOOL success + = (resizeSize.width > 0 && resizeSize.width <= resizeWidth && resizeSize.height <= textSize.height); + if (success) { + // Success. + textSize = resizeSize; + upperBound = (NSUInteger)textSize.width; + } else { + // Failure. + lowerBound = resizeWidth; + } + } + } + textSize.width = MIN(textSize.width, maxWidth); + return CGSizeCeil(textSize); +} + @end NS_ASSUME_NONNULL_END