UITableViewCell with UITextView height in iOS 7?
Asked Answered
D

12

121

How can I calculate the height of an UITableViewCell with an UITextView in it in iOS 7?

I found a lot of answers on similar questions, but sizeWithFont: takes part in every solution and this method is deprecated!

I know I have to use - (CGFloat)tableView:heightForRowAtIndexPath: but how do I calculate the height my TextView needs to display the whole text?

Derek answered 21/8, 2013 at 22:11 Comment(0)
M
428

First of all, it is very important to note, that there is a big difference between UITextView and UILabel when it comes to how text is rendered. Not only does UITextView have insets on all borders, but also the text layout inside it is slightly different.

Therefore, sizeWithFont: is a bad way to go for UITextViews.

Instead UITextView itself has a function called sizeThatFits: which will return the smallest size needed to display all contents of the UITextView inside a bounding box, that you can specify.

The following will work equally for both iOS 7 and older versions and as of right now does not include any methods, that are deprecated.


Simple Solution

- (CGFloat)textViewHeightForAttributedText: (NSAttributedString*)text andWidth: (CGFloat)width {
    UITextView *calculationView = [[UITextView alloc] init];
    [calculationView setAttributedText:text];
    CGSize size = [calculationView sizeThatFits:CGSizeMake(width, FLT_MAX)];
    return size.height;
}

This function will take a NSAttributedString and the desired width as a CGFloat and return the height needed


Detailed Solution

Since I have recently done something similar, I thought I would also share some solutions to the connected Issues I encountered. I hope it will help somebody.

This is far more in depth and will cover the following:

  • Of course: setting the height of a UITableViewCell based on the size needed to display the full contents of a contained UITextView
  • Respond to text changes (and animate the height changes of the row)
  • Keeping the cursor inside the visible area and keeping first responder on the UITextView when resizing the UITableViewCell while editing

If you are working with a static table view or you only have a known number of UITextViews, you can potentially make step 2 much simpler.

1. First, overwrite the heightForRowAtIndexPath:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // check here, if it is one of the cells, that needs to be resized
    // to the size of the contained UITextView
    if (  )             
        return [self textViewHeightForRowAtIndexPath:indexPath];
    else
    // return your normal height here:
            return 100.0;           
}

2. Define the function that calculated the needed height:

Add an NSMutableDictionary (in this example called textViews) as an instance variable to your UITableViewController subclass.

Use this dictionary to store references to the individual UITextViews like so:

(and yes, indexPaths are valid keys for dictionaries)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // Do you cell configuring ...

    [textViews setObject:cell.textView forKey:indexPath];
    [cell.textView setDelegate: self]; // Needed for step 3

    return cell;
}

This function will now calculate the actual height:

- (CGFloat)textViewHeightForRowAtIndexPath: (NSIndexPath*)indexPath {
    UITextView *calculationView = [textViews objectForKey: indexPath];
    CGFloat textViewWidth = calculationView.frame.size.width;
    if (!calculationView.attributedText) {
        // This will be needed on load, when the text view is not inited yet
        
        calculationView = [[UITextView alloc] init];
        calculationView.attributedText = // get the text from your datasource add attributes and insert here
        textViewWidth = 290.0; // Insert the width of your UITextViews or include calculations to set it accordingly
    }
    CGSize size = [calculationView sizeThatFits:CGSizeMake(textViewWidth, FLT_MAX)];
    return size.height;
}

3. Enable Resizing while Editing

For the next two functions, it is important, that the delegate of the UITextViews is set to your UITableViewController. If you need something else as the delegate, you can work around it by making the relevant calls from there or using the appropriate NSNotificationCenter hooks.

- (void)textViewDidChange:(UITextView *)textView {

    [self.tableView beginUpdates]; // This will cause an animated update of
    [self.tableView endUpdates];   // the height of your UITableViewCell

    // If the UITextView is not automatically resized (e.g. through autolayout 
    // constraints), resize it here

    [self scrollToCursorForTextView:textView]; // OPTIONAL: Follow cursor
}

4. Follow cursor while Editing

- (void)textViewDidBeginEditing:(UITextView *)textView {
    [self scrollToCursorForTextView:textView];
}

This will make the UITableView scroll to the position of the cursor, if it is not inside the visible Rect of the UITableView:

- (void)scrollToCursorForTextView: (UITextView*)textView {
    
    CGRect cursorRect = [textView caretRectForPosition:textView.selectedTextRange.start];
    
    cursorRect = [self.tableView convertRect:cursorRect fromView:textView];
    
    if (![self rectVisible:cursorRect]) {
        cursorRect.size.height += 8; // To add some space underneath the cursor
        [self.tableView scrollRectToVisible:cursorRect animated:YES];
    }
}

5. Adjust visible rect, by setting insets

While editing, parts of your UITableView may be covered by the Keyboard. If the tableviews insets are not adjusted, scrollToCursorForTextView: will not be able to scroll to your cursor, if it is at the bottom of the tableview.

- (void)keyboardWillShow:(NSNotification*)aNotification {
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(self.tableView.contentInset.top, 0.0, kbSize.height, 0.0);
    self.tableView.contentInset = contentInsets;
    self.tableView.scrollIndicatorInsets = contentInsets;
}

- (void)keyboardWillHide:(NSNotification*)aNotification {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.35];
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(self.tableView.contentInset.top, 0.0, 0.0, 0.0);
    self.tableView.contentInset = contentInsets;
    self.tableView.scrollIndicatorInsets = contentInsets;
    [UIView commitAnimations];
}

And last part:

Inside your view did load, sign up for the Notifications for Keyboard changes through NSNotificationCenter:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

Please don't get mad at me, for making this answer so long. While not all of it is needed to answer the question, I believe that there are other people who these directly related issues will be helpful to.


UPDATE:

As Dave Haupert pointed out, I forgot to include the rectVisible function:

- (BOOL)rectVisible: (CGRect)rect {
    CGRect visibleRect;
    visibleRect.origin = self.tableView.contentOffset;
    visibleRect.origin.y += self.tableView.contentInset.top;
    visibleRect.size = self.tableView.bounds.size;
    visibleRect.size.height -= self.tableView.contentInset.top + self.tableView.contentInset.bottom;
    
    return CGRectContainsRect(visibleRect, rect);
}

Also I noticed, that scrollToCursorForTextView: still included a direct reference to one of the TextFields in my project. If you have a problem with bodyTextView not being found, check the updated version of the function.

Mossberg answered 15/9, 2013 at 22:16 Comment(31)
That code is working well at all! It resize everything! But, my TextView always gets a height of 30px! Are there settings I am not allowed to set, or is there something I am not allowed in UITextView?Derek
Do I understand correctly? Your TableViewCell resizes, but the TextView doesn't? Look in step 3. I have put comments in the place that would be appropriate to put your textView resize code in. Let me know, if you need any more help.Mossberg
@TimBodeit As I'm updating an app to iOS7 today, I've run into crashes when using [tableview beginUpdates] / [tableview endUpdates] to update cell height - some assertions fail when creating the animation - mainly when the cell is not wholly visible on screen. Have you encountered anything similar?Maidservant
@TimBodeit No, both resizes, but not enough! I think its because of the attributedText! Is it just init or initWithString for the new AttirbutedString Object? I dont know how to deal with this object!Derek
@MyJBMe: Attributed String means, that you have to add attributes to it. Just setting your text, like with a regular string is not enough! For example, if you are using Helvetica Bold, font-size 20, you have to add that as an attribute. learn more about attributed stringsMossberg
@PeterSarnowski: No, until now, I have not yet had problems with assertion failures in table views. Neither in iOS 6, 7 final or any of the betas.Mossberg
Tim Bodeit - turns out it was not iOS7 but some old weirdness regarding section footer views.Maidservant
Doesn't work on iOS 7 for me. Bottom part of the text in text view displayed on the screen.Euratom
@iOSMonster: What do you mean by "bottom part of the text in text view displayed on the screen"? What is your problem?Mossberg
I mean I can see only last few lines of the text in text view, upper part of the textview hides behind the upper row in the table view.Euratom
@PeterSarnowski how did you fix your problem with [tableview beginUpdates] / [tableview endUpdates? i'm having the same problemBlackmon
@Blackmon - if you have footer views in your table, try to remove them and check if the problem persists.Maidservant
This solution does not seem to work on a copy and paste if the text is large, any ideas?Colincolinson
@Tim Bodeit, your solution works, thank you! But I think you should note in comment,that assigning of attributedText without specifying font,color and text alignment leads to the setting of default values of NSAttributedString attributes to the textView. In my case it cause getting different heights of the textview for the same text.Haematinic
Here links to textView.attributedString and NSAttributedString attributes default values.Haematinic
You can fix the copy and paste issue, by adjusting the table view's offset.Colincolinson
This is great @TimBodeit, but I am having issues with selectedTextRange when textViewDidBeginEditing is called. It always seems to return a range that is equivalent with the end of the string even if I tap at the top of the text view. The cursor gets placed properly but it seems selectedTextRange is inaccurate when the delegate is fired. This causes the cursor to be placed at the top and the view to scroll all the way to the bottom. Thoughts?Crinkle
This is the only right answer here. And guys, take care of the size returned by sizeThatFits:, because even you give the specific 'width' you want it to fit, sizeThatFits: will still return different width(This may cause - content moving up and down when typing). So set the returned size back to UITextView will solve the bug.Edwardedwardian
I've tried this, and it's not working. Apparently, method in heightForRowAtIndexPath is called first before cellForRowAtIndexPath, which means iVar textViews array always has 0 index at textViewHeightForRowAtIndexPath method. Anyone?Ninth
@Ninth have a look at textViewHeightForRowAtIndexPath:. If there is no corresponding entry in the textViews array, a temporary UITextView will be created for the operation.Mossberg
This answer was very helpful, but then i faced the problem that UITextViews with large text in it makes it cut some of the text.. Seems like an iOS 7/8 bug. Using an UILabel solved this!Greenbrier
One small correction: In textViewHeightForRowAtIndexPath you are defining CGFloat textViewWidth twice - which means it gets ignored when !calculationView.attributedText. Otherwise great code!Important
In my opinion, the above answer can be improved by replacing [self scrollToCursorForTextView:textView]; with [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES]; It would eliminate the step 4 and provides much more subtle animation.Zanezaneski
@VibhorGoyal No, actually this will not do. It probably would ensure that a small row is completely on screen at all times (not tested). Because I haven't tried it, I also can't say if the animation would look better or not. However, my sample code allows for unlimited amounts of text to be entered. If the user chooses to enter enough text for the row to become higher than the height of the screen between top bar and keyboard, your suggestion would stop working and the user wouldn't see what he is typing anymore.Mossberg
Please note: Yes, NSIndexPath can be used for dictionary keys, but, the NSIndexPath's created by tableView delegates sometimes won't work correctly when used with NSDictionary. No idea why, but the solution in the following link fixed it for me: https://mcmap.net/q/23755/-issues-using-nsindexpath-as-key-in-nsmutabledictionaryHambley
@TimBodeit: I am not able to get this working on iOS8. Please let me know how can this be fixed.Stenosis
On iOS8, when i tap on any textview inside tableview it scrolls to the top and then scrolls back to current position(jumps up and down) and for last row in table view the textview is not even visible on the screen it jump somewhere elseStenosis
@TimBodeit Did you get a chance to look at this on iOS8?Stenosis
@TimBodeit: This is thread is a touch old now, but I was just curious about 1 thing. In step 2 you grab cell.textView and I know textView is not a default property of UITableViewCell so I thought ok, you've probably just subclassed UITableViewCell and added this property. But when you dequeue your cell you should be getting a UITableViewCell object in return and not your subclassed cell, should there not be a cast? Unless you intended for that to happen in the //configure cell. Just curious about it. :) thanks for the awesome info!Flashlight
@ChrisShields True, the UITableViewCell type might be a bit misleading. Maybe something like TableViewCellSubclass would have been clearer.Mossberg
Can this be used for UITextField?Fortunetelling
G
37

There is a new function to replace sizeWithFont, which is boundingRectWithSize.

I added the following function to my project, which makes use of the new function on iOS7 and the old one on iOS lower than 7. It has basically the same syntax as sizeWithFont:

    -(CGSize)text:(NSString*)text sizeWithFont:(UIFont*)font constrainedToSize:(CGSize)size{
        if(IOS_NEWER_OR_EQUAL_TO_7){
            NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                              font, NSFontAttributeName,
                                              nil];

            CGRect frame = [text boundingRectWithSize:size
                                              options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                           attributes:attributesDictionary
                                              context:nil];

            return frame.size;
        }else{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
            return [text sizeWithFont:font constrainedToSize:size];
#pragma clang diagnostic pop
        }
    }

You can add that IOS_NEWER_OR_EQUAL_TO_7 on your prefix.pch file in your project as:

#define IOS_NEWER_OR_EQUAL_TO_7 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 7.0 )
Gorgonian answered 11/9, 2013 at 16:30 Comment(6)
My UITextViews still don't scale quite well and become scrollable when the text spans 3 lines; pastebin.com/Wh6vmBqhCharlenecharleroi
The second return statement also throws a deprecation warning in XCode.Charlenecharleroi
Are you also setting the size of the UItextView to the calculated size of the text, in cellForRowAtIndexPath? Also you shouldn't worry about the warning in the second return as it is only used when the app is ran on an iOS6 device in which the function is not deprecated.Gorgonian
Can you provide a simple example of how to use this function?Kayo
@Gorgonian Apple's documentation says that you must 'ceil' the result : In iOS 7 and later, this method returns fractional sizes (in the size component of the returned CGRect); to use a returned size to size views, you must use raise its value to the nearest higher integer using the ceil function.Contemn
@MartindeKeijzer The answer has been updated with #pragma marks to disable the warning, since a manual iOS version check is being done.Pinnacle
W
10

If you're using UITableViewAutomaticDimension I have a really simple (iOS 8 only) solution. In my case it's a static table view, but i guess you could adapt this for dynamic prototypes...

I have a constraint outlet for the text-view's height and I have implemented the following methods like this:

// Outlets

@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textViewHeight;


// Implementation

#pragma mark - Private Methods

- (void)updateTextViewHeight {
    self.textViewHeight.constant = self.textView.contentSize.height + self.textView.contentInset.top + self.textView.contentInset.bottom;
}

#pragma mark - View Controller Overrides

- (void)viewDidLoad {
    [super viewDidLoad];
    [self updateTextViewHeight];
}

#pragma mark - TableView Delegate & Datasource

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

#pragma mark - TextViewDelegate

- (void)textViewDidChange:(UITextView *)textView {
    [self.tableView beginUpdates];
    [self updateTextViewHeight];
    [self.tableView endUpdates];
}

But remember: the text view must be scrollable, and you must setup your constraints such that they work for automatic dimension:

  • setup all the view in the cell in relation to each other, with fixed heights (including the text view height, which you will change programatically)
  • the top most view has the top spacing and the bottom most view has the bottom spacing to the super view;

The most basic cell example is:

  • no other views in the cell except the textview
  • 0 margins around all sides of the text view and a predefined height constraint for the text view.
Wrestle answered 8/4, 2015 at 12:4 Comment(2)
the text view must NOT be scrollableMythomania
I'm getting the same size under updateTextviewHeight all the time. Looks like content size is wrong. Scrolling is disabled.Destrier
C
5

Tim Bodeit's answer is great. I used the code of Simple Solution to correctly get the height of the text view, and use that height in heightForRowAtIndexPath. But I don't use the rest of the answer to resize the text view. Instead, I write code to change the frame of text view in cellForRowAtIndexPath.

Everything is working in iOS 6 and below, but in iOS 7 the text in text view cannot be fully shown even though the frame of text view is indeed resized. (I'm not using Auto Layout). It should be the reason that in iOS 7 there's TextKit and the position of the text is controlled by NSTextContainer in UITextView. So in my case I need to add a line to set the someTextView in order to make it work correctly in iOS 7.

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        someTextView.textContainer.heightTracksTextView = YES;
    }

As the documentation said, what that property does is:

Controls whether the receiver adjusts the height of its bounding rectangle when its text view is resized. Default value: NO.

If leave it with the default value, after resize the frame of someTextView, the size of the textContainer is not changed, leading to the result that the text can only be displayed in the area before resizing.

And maybe it is needed to set the scrollEnabled = NO in case there's more than one textContainer, so that the text will reflow from one textContainer to the another.

Chough answered 16/10, 2013 at 8:4 Comment(0)
K
4

Here is one more solution that aims at simplicity and quick prototyping:

Setup:

  1. Table with prototype cells.
  2. Each cell contains dynamic sized UITextView w/ other contents.
  3. Prototype cells are associated with TableCell.h.
  4. UITableView is associated with TableViewController.h.

Solution:

(1) Add to TableViewController.m:

 // This is the method that determines the height of each cell.  
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // I am using a helper method here to get the text at a given cell.
    NSString *text = [self getTextAtIndex:indexPath];

    // Getting the height needed by the dynamic text view.
    CGSize size = [self frameForText:text sizeWithFont:nil constrainedToSize:CGSizeMake(300.f, CGFLOAT_MAX)];

    // Return the size of the current row.
    // 80 is the minimum height! Update accordingly - or else, cells are going to be too thin.
    return size.height + 80; 
}

// Think of this as some utility function that given text, calculates how much 
// space would be needed to fit that text.
- (CGSize)frameForText:(NSString *)text sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                          font, NSFontAttributeName,
                                          nil];
    CGRect frame = [text boundingRectWithSize:size
                                      options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                   attributes:attributesDictionary
                                      context:nil];

    // This contains both height and width, but we really care about height.
    return frame.size;
}

// Think of this as a source for the text to be rendered in the text view. 
// I used a dictionary to map indexPath to some dynamically fetched text.
- (NSString *) getTextAtIndex: (NSIndexPath *) indexPath
{
    return @"This is stubbed text - update it to return the text of the text view.";
}

(2) Add to TableCell.m:

// This method will be called when the cell is initialized from the storyboard
// prototype. 
- (void)awakeFromNib
{
    // Assuming TextView here is the text view in the cell. 
    TextView.scrollEnabled = YES;
}

Explanation:

So what's happening here is this: each text view is bound to the height of the table cells by vertical and horizontal constraints - that means when the table cell height increases, the text view increases its size as well. I used a modified version of @manecosta's code to calculate the required height of a text view to fit the given text in a cell. So that means given a text with X number of characters, frameForText: will return a size which will have a property size.height that matches the text view's required height.

Now, all that remains is the update the cell's height to match the required text view's height. And this is achieved at heightForRowAtIndexPath:. As noted in the comments, since size.height is only the height for the text view and not the entire cell, there should be some offset added to it. In the case of the example, this value was 80.

Kayo answered 21/10, 2013 at 3:20 Comment(2)
What does this 'dream.dream' stands for?Derek
@Derek sorry that was part of my own project - I have updated the code accordingly. dream.dream was the text that I was rendering in the text view.Kayo
M
3

One approach if you're using autolayout is to let the autolayout engine calculate the size for you. This isn't the most efficient approach but it is pretty convenient (and arguably the most accurate). It becomes more convenient as the complexity of the cell layout grows - e.g. suddenly you have two or more textviews/fields in the cell.

I answered a similar question with a complete sample for sizing tableview cells using auto layout, here:

How to resize superview to fit all subviews with autolayout?

Mignonne answered 16/9, 2013 at 15:51 Comment(0)
A
1

The complete smooth solution is as follows.

First, we need the cell class with a textView

@protocol TextInputTableViewCellDelegate <NSObject>
@optional
- (void)textInputTableViewCellTextWillChange:(TextInputTableViewCell *)cell;
- (void)textInputTableViewCellTextDidChange:(TextInputTableViewCell *)cell;
@end

@interface TextInputTableViewCell : UITableViewCell
@property (nonatomic, weak) id<TextInputTableViewCellDelegate> delegate;
@property (nonatomic, readonly) UITextView *textView;
@property (nonatomic) NSInteger minLines;
@property (nonatomic) CGFloat lastRelativeFrameOriginY;
@end


#import "TextInputTableViewCell.h"

@interface TextInputTableViewCell () <UITextViewDelegate> {
    NSLayoutConstraint *_heightConstraint;
}
@property (nonatomic) UITextView *textView;
@end

@implementation TextInputTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        _textView = [UITextView new];
        _textView.translatesAutoresizingMaskIntoConstraints = NO;
        _textView.delegate = self;
        _textView.scrollEnabled = NO;
        _textView.font = CELL_REG_FONT;
        _textView.textContainer.lineFragmentPadding = 0.0;
        _textView.textContainerInset = UIEdgeInsetsZero;
        [self.contentView addSubview:_textView];

        [self.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[view]-|" options:nil metrics:nil views:@{@"view": _textView}]];
        [self.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[view]-|" options:nil metrics:nil views:@{@"view": _textView}]];

        _heightConstraint = [NSLayoutConstraint constraintWithItem: _textView
                         attribute: NSLayoutAttributeHeight
                         relatedBy: NSLayoutRelationGreaterThanOrEqual
                         toItem: nil
                         attribute: NSLayoutAttributeNotAnAttribute
                         multiplier: 0.0
                         constant: (_textView.font.lineHeight + 15)];
        _heightConstraint.priority = UILayoutPriorityRequired - 1;
        [_textView addConstraint:_heightConstraint];
    }
    return self;
}

- (void)prepareForReuse {
    [super prepareForReuse];    
    self.minLines = 1;
}

- (void)setMinLines:(NSInteger)minLines {
    _heightConstraint.constant = minLines * _textView.font.lineHeight + 15;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([self.delegate respondsToSelector:@selector(textInputTableViewCellTextWillChange:)]) {
        [self.delegate textInputTableViewCellTextWillChange:self];
    }
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
    if ([self.delegate respondsToSelector:@selector(textInputTableViewCellTextDidChange:)]) {
        [self.delegate textInputTableViewCellTextDidChange:self];
    }
}

Next, we use it in the TableViewController

@interface SomeTableViewController () <TextInputTableViewCellDelegate>
@end

@implementation SomeTableViewController

. . . . . . . . . . . . . . . . . . . .

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    TextInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TextInputTableViewCellIdentifier forIndexPath:indexPath];
    cell.delegate = self;
    cell.minLines = 3;
    . . . . . . . . . .  
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (void)textInputTableViewCellWillChange:(TextInputTableViewCell *)cell {
    cell.lastRelativeFrameOriginY = cell.frame.origin.y - self.tableView.contentOffset.y;
}

- (void)textInputTableViewCellTextDidChange:(TextInputTableViewCell *)cell {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    [UIView performWithoutAnimation:^{
        [self.tableView moveRowAtIndexPath:indexPath toIndexPath:indexPath];
    }];

    CGFloat contentOffsetY = cell.frame.origin.y - cell.lastRelativeFrameOriginY;
    self.tableView.contentOffset = CGPointMake(self.tableView.contentOffset.x, contentOffsetY);

    CGRect caretRect = [cell.textView caretRectForPosition:cell.textView.selectedTextRange.start];
    caretRect = [self.tableView convertRect:caretRect fromView:cell.textView];

    CGRect visibleRect = self.tableView.bounds;
    visibleRect.origin.y += self.tableView.contentInset.top;
    visibleRect.size.height -= self.tableView.contentInset.top + self.tableView.contentInset.bottom;
    BOOL res = CGRectContainsRect(visibleRect, caretRect);
    if (!res) {
        caretRect.size.height += 5;
        [self.tableView scrollRectToVisible:caretRect animated:NO];
    }
}
@end
  • Here minLines allows to set minimum height for the textView (to resist height minimizing by AutoLayout with UITableViewAutomaticDimension).

  • moveRowAtIndexPath:indexPath: with the same indexPath starts tableViewCell height re-calculation and re-layout.

  • performWithoutAnimation: removes side-effect (tableView content offset jumping on starting new line while typing).

  • It is important to preserve relativeFrameOriginY (not contentOffsetY!) during cell update because contentSize of the cells before the current cell could be change by autoLayout calculus in unexpected way. It removes visual jumps on system hyphenation while typing long words.

  • Note that you shouldn't set the property estimatedRowHeight! The following doesn't work

    self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
    

    Use only tableViewDelegate method.

==========================================================================

If one doesn't mind against weak binding between tableView and tableViewCell and updating geometry of the tableView from tableViewCell, it is possible to upgrade TextInputTableViewCell class above:

@interface TextInputTableViewCell : UITableViewCell
@property (nonatomic, weak) id<TextInputTableViewCellDelegate> delegate;
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, readonly) UITextView *textView;
@property (nonatomic) NSInteger minLines;
@end


#import "TextInputTableViewCell.h"

@interface TextInputTableViewCell () <UITextViewDelegate> {
    NSLayoutConstraint *_heightConstraint;
    CGFloat _lastRelativeFrameOriginY;
}
@property (nonatomic) UITextView *textView;
@end

@implementation TextInputTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        _textView = [UITextView new];
        _textView.translatesAutoresizingMaskIntoConstraints = NO;
        _textView.delegate = self;
        _textView.scrollEnabled = NO;
        _textView.font = CELL_REG_FONT;
        _textView.textContainer.lineFragmentPadding = 0.0;
        _textView.textContainerInset = UIEdgeInsetsZero;
        [self.contentView addSubview:_textView];

        [self.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[view]-|" options:nil metrics:nil views:@{@"view": _textView}]];
        [self.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[view]-|" options:nil metrics:nil views:@{@"view": _textView}]];

        _heightConstraint = [NSLayoutConstraint constraintWithItem: _textView
                         attribute: NSLayoutAttributeHeight
                         relatedBy: NSLayoutRelationGreaterThanOrEqual
                         toItem: nil
                         attribute: NSLayoutAttributeNotAnAttribute
                         multiplier: 0.0
                         constant: (_textView.font.lineHeight + 15)];
        _heightConstraint.priority = UILayoutPriorityRequired - 1;
        [_textView addConstraint:_heightConstraint];
    }
    return self;
}

- (void)prepareForReuse {
    [super prepareForReuse];    
    self.minLines = 1;
    self.tableView = nil;
}

- (void)setMinLines:(NSInteger)minLines {
    _heightConstraint.constant = minLines * _textView.font.lineHeight + 15;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    _lastRelativeFrameOriginY = self.frame.origin.y - self.tableView.contentOffset.y;
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView {

    NSIndexPath *indexPath = [self.tableView indexPathForCell:self];
    if (indexPath == nil) return;

    [UIView performWithoutAnimation:^{
        [self.tableView moveRowAtIndexPath:indexPath toIndexPath:indexPath];
    }];

    CGFloat contentOffsetY = self.frame.origin.y - _lastRelativeFrameOriginY;
    self.tableView.contentOffset = CGPointMake(self.tableView.contentOffset.x, contentOffsetY);

    CGRect caretRect = [self.textView caretRectForPosition:self.textView.selectedTextRange.start];
    caretRect = [self.tableView convertRect:caretRect fromView:self.textView];

    CGRect visibleRect = self.tableView.bounds;
    visibleRect.origin.y += self.tableView.contentInset.top;
    visibleRect.size.height -= self.tableView.contentInset.top + self.tableView.contentInset.bottom;

    BOOL res = CGRectContainsRect(visibleRect, caretRect);
    if (!res) {
        caretRect.size.height += 5;
        [self.tableView scrollRectToVisible:caretRect animated:NO];
    }
}
@end
Avidin answered 4/6, 2016 at 18:49 Comment(0)
E
1
  1. Put UILabel behind your UITextView.
  2. Use this answer: https://mcmap.net/q/23756/-dynamically-increase-height-of-uilabel-amp-tableview-cell to UILabel you created
  3. Give them same constraints and fonts
  4. Set them same text;

Your cell's height will calculate by UILabel's content, but all text will be showed by TextField.

Eisenhart answered 5/10, 2017 at 10:47 Comment(0)
S
0
UITextView *txtDescLandscape=[[UITextView alloc] initWithFrame:CGRectMake(2,20,310,2)];

    txtDescLandscape.editable =NO;
    txtDescLandscape.textAlignment =UITextAlignmentLeft;
    [txtDescLandscape setFont:[UIFont fontWithName:@"ArialMT" size:15]];
    txtDescLandscape.text =[objImage valueForKey:@"imgdescription"];
    txtDescLandscape.text =[txtDescLandscape.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    [txtDescLandscape sizeToFit];
    [headerView addSubview:txtDescLandscape];

    CGRect txtViewlandscpframe = txtDescLandscape.frame;
    txtViewlandscpframe.size.height = txtDescLandscape.contentSize.height;
    txtDescLandscape.frame = txtViewlandscpframe;

i think this way you can count the height of your text view and then resize your tableview cell according to that height so that you can show full text on cell

Selfseeker answered 22/8, 2013 at 12:7 Comment(0)
S
0

Swift version

func textViewHeightForAttributedText(text: NSAttributedString, andWidth width: CGFloat) -> CGFloat {
    let calculationView = UITextView()
    calculationView.attributedText = text
    let size = calculationView.sizeThatFits(CGSize(width: width, height: CGFloat.max))
    return size.height
}
Sexennial answered 25/4, 2016 at 9:33 Comment(0)
C
0

If you want to automatically adjust UITableViewCell's height based on the height of the inner UITextView's height. See my answer here: https://mcmap.net/q/23757/-uitableviewcell-uitextview-with-dynamic-height

The solution is quite simple and should work since iOS 7. Make sure that the Scrolling Enabled option is turned off for the UITextView inside the UITableViewCell in the StoryBoard.

Then in your UITableViewController's viewDidLoad() set the tableView.rowHeight = UITableViewAutomaticDimension and tableView.estimatedRowHeight > 0 such as:

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 44.0
}

That's it. UITableViewCell's height will be automatically adjusted based on the inner UITextView's height.

Conjugation answered 28/8, 2017 at 12:2 Comment(0)
W
-2

For iOS 8 and above you can just use

your_tablview.estimatedrowheight= minheight you want

your_tableview.rowheight=UItableviewautomaticDimension
Wulf answered 14/7, 2016 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.