From 3cb6dcd32a2f2989cf0a9eeb049f5673ac275a9c Mon Sep 17 00:00:00 2001 From: Ignacio Romero Zurbuchen Date: Tue, 20 Oct 2015 14:31:45 -0700 Subject: [PATCH] Fixes the `contentSize` to never be higher than the `bounds` when there is only 1 line of content. This helps avoiding unnecessary scrolling in the text view when setting for larger and smaller font sizes. --- Source/SLKTextView.m | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/Source/SLKTextView.m b/Source/SLKTextView.m index 65f23a86..6263dd99 100644 --- a/Source/SLKTextView.m +++ b/Source/SLKTextView.m @@ -162,10 +162,26 @@ - (UIColor *)placeholderColor - (NSUInteger)numberOfLines { - CGFloat contentHeight = self.contentSize.height; + CGSize contentSize = self.contentSize; + + CGFloat contentHeight = contentSize.height; contentHeight -= self.textContainerInset.top + self.textContainerInset.bottom; - - return fabs(contentHeight/self.font.lineHeight); + + NSUInteger lines = fabs(contentHeight/self.font.lineHeight); + + // This helps preventing the content's height to be larger that the bounds' height + // Avoiding this way to have unnecessary scrolling in the text view when there is only 1 line of content + if (lines == 1 && contentSize.height > self.bounds.size.height) { + contentSize.height = self.bounds.size.height; + self.contentSize = contentSize; + } + + // Let's fallback to the minimum line count + if (lines == 0) { + lines = 1; + } + + return lines; } - (NSUInteger)maxNumberOfLines