Skip to content

Commit 3cb6dcd

Browse files
Ignacio Romero ZurbuchenIgnacio Romero
Ignacio Romero Zurbuchen
authored and
Ignacio Romero
committed
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.
1 parent 06fa541 commit 3cb6dcd

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

Source/SLKTextView.m

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,26 @@ - (UIColor *)placeholderColor
162162

163163
- (NSUInteger)numberOfLines
164164
{
165-
CGFloat contentHeight = self.contentSize.height;
165+
CGSize contentSize = self.contentSize;
166+
167+
CGFloat contentHeight = contentSize.height;
166168
contentHeight -= self.textContainerInset.top + self.textContainerInset.bottom;
167-
168-
return fabs(contentHeight/self.font.lineHeight);
169+
170+
NSUInteger lines = fabs(contentHeight/self.font.lineHeight);
171+
172+
// This helps preventing the content's height to be larger that the bounds' height
173+
// Avoiding this way to have unnecessary scrolling in the text view when there is only 1 line of content
174+
if (lines == 1 && contentSize.height > self.bounds.size.height) {
175+
contentSize.height = self.bounds.size.height;
176+
self.contentSize = contentSize;
177+
}
178+
179+
// Let's fallback to the minimum line count
180+
if (lines == 0) {
181+
lines = 1;
182+
}
183+
184+
return lines;
169185
}
170186

171187
- (NSUInteger)maxNumberOfLines

0 commit comments

Comments
 (0)