5

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:

enter image description here

Am I missing something obvious?

UPDATED (frame background):

enter image description here

Paul Peelen
  • 10,073
  • 15
  • 85
  • 168
Allen
  • 3,601
  • 10
  • 40
  • 59
  • possible duplicate of [Weird thing occurs when using sizetofit of a UITextView in IOS7](http://stackoverflow.com/questions/18928832/weird-thing-occurs-when-using-sizetofit-of-a-uitextview-in-ios7) – Jeremy Jan 08 '14 at 14:21
  • not a duplicate...it's a different problem – Allen Jan 08 '14 at 14:22
  • @Jeremy please remove the duplicate tag...that one is saying that the text has been cut off. Mine is saying that the frame is not adjusting to multiple lines of text. – Allen Jan 08 '14 at 14:26
  • Set a background color to your textview and see where the frame is displayed. – Putz1103 Jan 08 '14 at 14:34
  • self.textView.backgroundColor = [UIColor grayColor]; with picture! – Allen Jan 08 '14 at 14:38
  • if you do not want to use the scroll to a UITextView, but you want the UITextView is as big as the content, please consider using a UILabel (IMHO) – Ilario Jan 08 '14 at 14:47
  • can you provide an answer with uilabel? because I've tried autoresizing for it as well and it doesn't work – Allen Jan 08 '14 at 14:48

4 Answers4

6

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];

  };

enter image description here

Ilario
  • 5,979
  • 2
  • 32
  • 46
5

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.

Mike
  • 51
  • 1
  • 1
  • 1
    Can someone explain why this works in viewdidappear but not in viewdidlayoutsubviews? – ullstrm May 19 '15 at 14:32
  • I think this is best solution, easy to understand why sizeToFit not effect to Textview, because inside textview is a scrollview. So we need to update constraint height instead for sizeToFit. – Linh Nguyen May 24 '16 at 06:35
  • @Spoek: This Solution is work for both of viewdidappear or viewdidlayoutsubviews. I have just checked. – Linh Nguyen May 24 '16 at 06:41
1

Have you tried using a UIViewContentMode ?

For example:

[self.textView setContentMode:UIViewContentModeScaleToFill];
andrewbuilder
  • 3,629
  • 2
  • 24
  • 46
  • 2
    @Allen you down voted me for a suggestion? Bad day perhaps? Remind me not to offer any suggestions again??? – andrewbuilder Jan 08 '14 at 15:35
  • 1
    sorry wasn't personal! just want the working answer to float to the top – Allen Jan 09 '14 at 13:32
  • 4
    Yes understand @Allen, but for newcomers like me it has a very real effect on our ability to become more involved in this forum, regardless of our ability or enthusiasm. A more effective method for the working answer to float to the top is for users to upvote it. FYI: The next suggestion I had in mind was in fact related to the answer that assisted you, and is a link to an extremely good Q&A on stack overflow that I have added as a favourite... [Vertically align text within a UILabel](http://stackoverflow.com/questions/1054558/vertically-align-text-within-a-uilabel). – andrewbuilder Jan 10 '14 at 02:54
  • I'm working in some old legacy code, and long story short, your answer helped me realize that I needed to set the contentMode of one textView to match that of another to get my UITextView to size correctly. Thanks!! – John Jacecko Dec 08 '22 at 04:05
-2

Try adjusting the frame size manually.

self.textView.text = lorum;
CGSize size = [lorum sizeWithFont:self.textView.font
                         constrainedToSize:CGSizeMake(self.textView.frame.size.width, 9999)
                             lineBreakMode:UILineBreakModeWordWrap];
CGRect frame = self.textView.frame;
frame.size.width = size.width;
frame.size.height = size.height;
self.textView.frame = frame;

That should set your textview's size to the size of the text (constrained to the width of your textView). The origin should remain the same.

As far as I know sizeToFit only shrinks the frame, it will not expand it. So if your frame is originally only one line tall it will remain only one line tall.

Alternatively, you could set the textView's frame to your entire window, then use sizeToFit and that should work properly.

EDIT:

To avoid the deprecated function you could also use

CGSize size = [lorum sizeWithAttributes:@{NSFontAttributeName:self.textView.font}];

or

CGRect rect = [text boundingRectWithSize:(CGSize){ .width = self.textView.frame.size.width, .height = CGFLOAT_MAX } options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName = self.textView.font } context:nil];
Putz1103
  • 6,211
  • 1
  • 18
  • 25
  • doesn't work, the method sizeWithFont... is deprecated in iOS7 – Allen Jan 08 '14 at 14:23
  • 1
    Then use `[lorum sizeWithAttributes:@{NSFontAttributeName:self.textView.font}];` or `[text boundingRectWithSize:(CGSize){ .width = self.textView.frame.size.width, .height = CGFLOAT_MAX } options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName = self.textView.font } context:nil];` – Putz1103 Jan 08 '14 at 14:25
  • property size not found on textView (self.textView.size = size;). frame.size is not assignable – Allen Jan 08 '14 at 14:27
  • self.textView.size.width = size.width; self.textView.size.height = size.height; aren't working – Allen Jan 08 '14 at 14:29
  • 1
    Update answer with more options and fixed typos. – Putz1103 Jan 08 '14 at 14:30
  • sorry mate doesn't work. (tried this before I posted as well). – Allen Jan 08 '14 at 14:33
  • 4
    It is often useful to show things that you have tried that have not worked, so that others don't give you an answer that you already know doesn't work for your situation. – Putz1103 Jan 08 '14 at 14:35