Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextEditor sample: Allow scrolling when viewUsedSize > contentSize #68

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,24 @@ - (void)layoutSubviews;
// First make sure the text is the right width so that it can calculate the right used height
CGRect textFrame = _textView.frame;
if (textFrame.size.width != bounds.size.width) {
_textView.frame = CGRectMake(0, 0, bounds.size.width, textFrame.size.height);
textFrame.size.width = bounds.size.width;
}

// Then ensure the height is large enough to span the text (or our height).
CGSize usedSize = _textView.viewUsedSize;
CGFloat height = MAX(bounds.size.height, usedSize.height);
if (height != textFrame.size.height) {
_textView.frame = CGRectMake(0, 0, bounds.size.width, height);
textFrame.size.height = height;
}

// Adjust the textView frame only if needed
if (!CGRectEqualToRect(_textView.frame, textFrame)) {
_textView.frame = textFrame;
}

// Adjust the contentSize so we can scroll
if (!CGSizeEqualToSize(self.contentSize, textFrame.size)) {
self.contentSize = textFrame.size;
}
}

Expand Down