Skip to content

Instantly share code, notes, and snippets.

@JigarM
Created July 4, 2016 11:51
Show Gist options
  • Save JigarM/559930b18c34ef730e9a2af142b1436e to your computer and use it in GitHub Desktop.
Save JigarM/559930b18c34ef730e9a2af142b1436e to your computer and use it in GitHub Desktop.
Objective-C Auto Set Height of UITextView Raw
// leave the width at 300 otherwise the height wont measure correctly
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10.0f, 0.0f, 300.0f, 100.0f)];
// add text to the UITextView first so we know what size to make it
textView.text = [_dictSelected objectForKey:@"body"];
// get the size of the UITextView based on what it would be with the text
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
textView.frame = newFrame;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment