[Fast hint] Resize height of UITextView to its content
That's a common problem, many people ask to me how to make it simple
Ok, let's go to more one fast hint
let's go coding
in your - (void)viewDidLoad create a UITextField and set a text
- (void)viewDidLoad
{
UITextView *myTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 300, 50)];
[myTextView set:@"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus pellentesque orci ac quam cursus semper."];
[self.view addSubview:myTextView];
[super viewDidLoad];
}
execute... and result got like this
Ok, but the text can be a bigger, and a UITextView get a scrollbar
it's strange for user experience
Ok, let's add a little code to solve that problem
- (void)viewDidLoad
{
UITextView *myTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 300, 50)];
[myTextView setText:@"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi eleifend urna non velit vulputate porttitor. Aenean odio enim, consequat at euismod sed, congue in massa. Fusce volutpat laoreet elementum. Nam purus nisi, tristique et adipiscing sed, egestas vel libero. Nam iaculis placerat lectus at lacinia. Proin aliquet arcu eu leo."];
CGRect frame = myTextView.frame;
frame.size.height = myTextView.contentSize.height;
myTextView.frame = frame;
[self.view addSubview:myTextView];
[super viewDidLoad];
}
and now...
UITextView resize auto, test with differents size of text
For this hint is all up to the next
Thanks