0

I'm trying to update a TextView dependent on the text that is inside it. I need to increase the height to the bottom direction when a user enters more text. than can fit on one line.

I'm currently using the following code:

-(void)textViewDidChange:(UITextView *)textView{
    CGRect newFrame = self.frame;
    newFrame.size.height = [self textViewHeightForAttributedText:
                       [[NSMutableAttributedString alloc] initWithString:self.text]
                       andWidth:self.frame.size.width];

    [self setNeedsLayout];

}

- (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;
}

However, this isn't updating the height of the UITextView. I have set the delegate of the UITextView. This code is written in a subclass in a subclass of UITextView.

bdv
  • 1,154
  • 2
  • 19
  • 42

3 Answers3

0

It looks like you're not actually changing the height of the textView, you're just creating a new frame with the same dimensions of the textView, resizing the new frame, then doing nothing with it.

Try adding:

textView.frame = newFrame;
JoGoFo
  • 1,928
  • 14
  • 31
  • I see, I added the line but unfortunately this doesn't help. Even though I think you're right so I'll leave that line in :) – bdv Nov 26 '14 at 00:56
  • Can you make sure textViewDidChange is being called? Pop an NSLog statement in there and make sure you see the console message each time a key is pressed. – JoGoFo Nov 26 '14 at 00:58
  • Yeah I'm sure, double checked that just yet. – bdv Nov 26 '14 at 00:59
  • What sort of class is this object i.e. what is 'self'. Is it inheriting from a UITextView, and is the UITextView you are trying to update an instance of this class? Or is 'self' a view controller? In which case, what is self.frame? – JoGoFo Nov 26 '14 at 01:08
  • self is the UITextView. I'll mention that this is written in a subclass of UITextView – bdv Nov 26 '14 at 01:10
  • Are you using autolayout? If so, you could just call [self sizeToFit] inside textViewDidChange. If not, take a look at this answer: http://stackoverflow.com/a/2487402/3642906 – JoGoFo Nov 26 '14 at 01:26
  • Yes, I'm using autolayout. Somehow, none of these solutions seem to work. As for the answer you're linking to, that doesn't work for a subclass of `UITextField` (hence the constraint). Is there maybe something obvious that I should take into account with this subclass that I'm not? – bdv Nov 26 '14 at 01:36
  • What's the reason for using a subclass? Are you overriding any methods which might be relevant? Or maybe there's some funny stuff going on with the textview being a delegate of itself? Maybe try setting the delegate to your viewcontroller and moving the relevant functions there. – JoGoFo Nov 26 '14 at 01:46
  • I need to use a UITextView with that kind of behaviour more then once in my app, so therefore the subclass. I'll try and see if moving the delegate methods have anything useful! – bdv Nov 26 '14 at 01:48
0

You can use Coco-control named HPGrowingTextView for i OS.Similar to the one Apple uses in the SMS.

Go through this link.https://www.cocoacontrols.com/controls/hpgrowingtextview

0
- (void)textViewDidChange:(UITextView *)textView
{
    UIFont *myFont = [UIFont systemFontOfSize:14];
    CGSize size =   [self sizeOfText:textView.text widthOfTextView:TextviewWidth withFont:myFont];
    NSLog(@"Height : %f", size.height);
    txtView_Details.frame = CGRectMake(txtView_Details.frame.origin.x, txtView_Details.frame.origin.y, txtView_Details.frame.size.width, size.height);
}

-(CGSize)sizeOfText:(NSString *)textToMesure widthOfTextView:(CGFloat)width withFont:(UIFont*)font
{
    CGSize ts = [textToMesure sizeWithFont:font constrainedToSize:CGSizeMake(width-20.0, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
    return ts;
}
Hardik Kardani
  • 576
  • 3
  • 24