It's weird I've been looking everywhere for this answer but nothing seems to work!
I have simple UITextView
that I'm trying to resize in a viewController according to its contents:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *lorum = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";
self.textView.text = lorum;
self.textView.scrollEnabled = NO;
[self.textView sizeToFit];
[self.textView layoutIfNeeded];
}
But this is all I end up with:
Am I missing something obvious?
UPDATED (frame background):
as i wrote in comment, in my opinion if you dont want use scroll of UITextView i suggest to use a simple UILabel.. this is code:
EDIT: if you want use UITextView for some reason like editable text or selectable text, you can use the same code with some modifications (i add this modifications in code like comments)
EDIT2 if your label is in storyboard check constraints or remove autolayout
.h
@interface ViewController : UIViewController {
IBOutlet UILabel *label;
}
.m
- (void)viewDidLoad {
[super viewDidLoad];
// UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor lightGrayColor];
NSString *lorum = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";
//for use labrect with UITextView you set the font of UITextView:
//label.font = [UIFont systemFontOfSize:17];
CGSize maxSize = CGSizeMake(320, 410);
CGRect labrect = [lorum boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:label.font} context:Nil];
label.text = lorum;
//for use UITextView you should comment the line under
label.numberOfLines = 0;
label.frame = CGRectMake(0, 30, 320, labrect.size.height);
// [self.view addSubview:label];
};
https://medium.com/ios-os-x-development/102e73853fbc
Take a look at this solution. It worked for me!
All you have to do is add a height constraint on storyboard with an outlet in your view controller. Then add the following lines to the viewDidAppear method:
CGSize size = self.textView.contentSize; self.textViewHeightConstraint.constant = size.height;
There is a sample project if you follow the link.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With