14

I am using this code

CGRect frame = self.mytext.frame;
frame.size.height = self.mytext.contentSize.height;
self.mytext.frame = frame;

But it doesn`t work in iOS 7. Does anyone know why or have the same problem?

EDITED:

Sorry. I made an UIView, a UITextView and a UIScrollView so i can load the text in my uitextview expand.

I used this code in viewDidLoad

CGRect frame = self.moreDetailsTextView.frame;
frame.size.height = self.moreDetailsTextView.contentSize.height;
self.moreDetailsTextView.frame = frame;

CGRect moreViewFrame =  self.moreDetailsView.frame;
moreViewFrame.size.height = frame.size.height + 270;
self.moreDetailsView.frame = moreViewFrame;

int scrollViewHeight =  moreViewFrame.size.height + 235;
self.scrollView.contentSize = CGSizeMake(320, scrollViewHeight);

This was working fine in ios6, but now with xcode5 and ios7 the uitextview does not expand when i test the app in the simulator.

Osvaldo Cipriano
  • 313
  • 1
  • 2
  • 11

9 Answers9

22

Noticed in IOS7 sizeToFit wasn't working also - perhaps the solution may help you too

[UITEXTVIEW sizeToFit];
[UITEXTVIEW layoutIfNeeded];
Nick Wood
  • 680
  • 6
  • 10
  • Tested working, if run before getting the content size frame height. Note that the previous 3-line solution (in the question) only affects the height, but this will "shrink-wrap" the text properly. Just set the frame.width to the one used when creating the UITextView. I preferred this one because it lets me extend existing code easily. – Henrik Erlandsson Sep 27 '13 at 11:48
9

Finally i did it.

If anyone is having the same problem just had this code before

    [_moreDetailsTextView sizeToFit];
Osvaldo Cipriano
  • 313
  • 1
  • 2
  • 11
5

To address this exact problem I made an auto-layout based light-weight UITextView subclass which automatically grows and shrinks based on the size of user input and can be constrained by maximal and minimal height - all without a single line of code.

Made primarily for use in Interface builder and only works with Auto layout

https://github.com/MatejBalantic/MBAutoGrowingTextView

Matej Balantič
  • 1,627
  • 18
  • 21
  • 1
    Dude, your stuff says "All rights reserved" so it's no good to me or anyone really for that matter. I would rather give you credit for it than to benchmark yours. But now I can't use the file as is since it says All Rights Reserved. So you may want to fix that. – Katedral Pillon Jan 24 '15 at 17:54
  • 4
    @KatedralPillon The code in that repository is licensed under the MIT license, one of the most unrestrictive open source licenses available. Source: https://github.com/MatejBalantic/MBAutoGrowingTextView/blob/master/LICENSE.txt – Code Commander Feb 04 '15 at 18:00
2
[UITextView sizeToFit];

Using sizeToFit resizes the textview but cuts some text from the bottom. Any idea how to resolve this?

Solution:

Finally the method described at the following thread worked. https://stackoverflow.com/a/18818036/1869452

Community
  • 1
  • 1
Namit Gupta
  • 796
  • 9
  • 20
1

Neither of answers worked for me. I have editable UITextView that I want to dynamically expand in height as the text grows in multiple lines.

It worked fine for iOS6 but I had problems on iOS7 with scrolling the text view in order to keep the cursor visible.

The solution for me was to put bottom inset for text view on iOS7. Like this:

_textView.contentInset = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0") ? UIEdgeInsetsMake(0, 0, DEFAULT_FONT_SIZE, 0) : UIEdgeInsetsZero;

Satisfiable results. Good luck.

AndroC
  • 4,758
  • 2
  • 46
  • 69
1

Recently I have found this CSGrowingTextView that sizes while you type, maybe it can help: https://github.com/cloverstudio/CSGrowingTextView

Josip B.
  • 2,434
  • 1
  • 25
  • 30
0

Get the new size of the textView using this method

CGSize sz = [_textView.text sizeWithFont:_textView.font]

in case that didn't work very well with the height,get the width you just got from the preivous method and use in the next method to get the appropriate height you need

- (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text andWidth:(CGFloat)width
{
    UITextView *textView = [[UITextView alloc] init];

    [textView performSelectorOnMainThread:@selector(setAttributedText:)
                                    withObject:text
                                 waitUntilDone:YES];

    CGSize size = [textView sizeThatFits:CGSizeMake(width, FLT_MAX)];
    return size.height;
}

Note : if there was any animation in the view,all the changes made in the view.frame will be discarded as the view will be redrawn again. So it would be better to deal with the constraints than dealing with the frame itself as the changes you will make in the constraint will be permanent even if the view got refreshed.

create IBoutlet of a constraint and connect it to your class like that

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *your_constraint;

change the value of the contraint just like the way you change the view.frame parameters like that

            _constraintHeaderTitle.constant = THE_NEW_VALUE;
Mohammad Allam
  • 258
  • 2
  • 14
0

GrowingTextViewHandler is an NSObject subclass which resizes text view as user types text. Handling resize via NSObject subclass is more useful rather than subclassing UITextView, because it will work for any subclass of UITextView. Here is the sample usage.

@interface ViewController ()<UITextViewDelegate>

 @property (weak, nonatomic) IBOutlet UITextView *textView;
 @property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
 @property (strong, nonatomic) GrowingTextViewHandler *handler;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  self.handler = [[GrowingTextViewHandler alloc]initWithTextView:self.textView withHeightConstraint:self.heightConstraint];
  [self.handler updateMinimumNumberOfLines:3 andMaximumNumberOfLine:8];
 }

- (void)textViewDidChange:(UITextView *)textView {
  [self.handler resizeTextViewWithAnimation:YES];
}
@end
hsusmita
  • 282
  • 3
  • 16
0

I've worked on a complete solution to solve this on iOS 9.3.1. Find it here.

enter image description here

Community
  • 1
  • 1
Andres Canella
  • 3,706
  • 1
  • 35
  • 47