2

I want to get deleted text in UITextView.

Because I have to decrease UITextView, when '\n' deleted.

However, shouldChangeTextInRange delegate method is only return last string.

So, For example

UITextView.text = @"ABCD\nEFG";

and user delete text is @"D\nEFG".

Then, returned text is not '\n'. so I do not know whether the line number should be reduced. help me!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
kkbpower
  • 101
  • 6

3 Answers3

0

Simple Solution

- (void)textViewDidChange:(UITextView *)textView
{
    CGFloat fixedWidth = textView.frame.size.width;
    CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
    CGRect newFrame = textView.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
    textView.frame = newFrame;
}

With this following code, you can change the height of your UITextView depending of a fixed width (it's working on iOS 7 and previous version) :

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

With this function, you will take a NSAttributedString and a fixed width to return the height needed. If you want to calculate the frame from a text with a specific font you, need to use the following code :

- (CGSize)text:(NSString *)text sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
    if ([text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)])‌
    {
        CGRect frame = [text boundingRectWithSize:size
                                          options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                       attributes:@{NSFontAttributeName:font}
                                          context:nil];
        return frame.size;
    }
    else
    {
        return [text sizeWithFont:font constrainedToSize:size];
    }
}

This worked for me for iOS6 and 7:

CGSize textViewSize = [self.myTextView sizeThatFits:CGSizeMake(self.myTextView.frame.size.width, FLT_MAX)];
    self.myTextView.height = textViewSize.height;
Mehul
  • 3,033
  • 23
  • 37
0

You know the range of text that is going to be changed within shouldChangeTextInRange.

Before you return YES from shouldChangeTextInRange, you can scan the characters within that range to see if there is a "\n" or "\r" character there and then you can decrease the line number by the number of new line characters found in the range.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
0

You can use the below code to get the height of UItextView -

CGSize textViewSize = [text sizeWithFont:[UIFont fontWithName:@"font_name" size:font_size] 
                   constrainedToSize:CGSizeMake(WIDHT_OF_VIEW, FLT_MAX) 
                   lineBreakMode:UILineBreakModeTailTruncation];

Replace font_name and font_size with actual value that you are using in UITextView. and Replace WIDHT_OF_VIEW with width of your UITextView.

You can get the height of UITextView using below code-
textViewSize.height

prema janoti
  • 159
  • 1
  • 4