user7018875
user7018875

Reputation: 160

How To resize UITextView when it has only single line From the multiline With View

I have made one UITextview Custom control with View.It is working fine. When I enter for new line text view increases with view(Dark Blue).After this TextViell will working in form of scrolling.

Code is

-(void)setTextViewHeight
{
    self.oldFrameHeight = self.commentTextView.frame.size.height;
    CGFloat fixedWidth = self.commentTextView.frame.size.width;
    CGSize newSize = [self.commentTextView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
    CGRect newFrame = self.commentTextView.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
    self.commentTextView.frame = newFrame;
    self.newFrameHeight = newFrame.size.height;
    NSLog(@"Old Frame %f",self.oldFrameHeight);
    NSLog(@"New Frame %f",self.newFrameHeight);
}
- (void)textViewDidChange:(UITextView *)textView
{


    self.oldFrameHeight = textView.frame.size.height;


    if(!(self.oldFrameHeight >= 44))
    {
        [self setTextViewHeight];

        if (self.newFrameHeight  > self.oldFrameHeight)
        {
            [self sabViewFrameChange];
        }
    }
    else if ([textView.text isEqualToString:@""])
    {
        [self.commentTextView setFrame:CGRectMake(60, 10,self.frame.size.width - 130, 30)];

        UIViewController *controller=(UIViewController *)self.delegate;
        self.frame =  CGRectMake(0,controller.view.frame.size.height - 50, controller.view.frame.size.width,50);
        self.height = 50;
    }
    else
    {
        self.commentTextView.scrollEnabled = YES;
    }
}

My Question Is

When I have removed second line from the UITextView the size will remain as it is,I did not decrease with line.

Please help me. Thanks in advance.

Image is

1) With more then two line

enter image description here

2) With single line

enter image description here

How can I resolve it ?Any one have any idea?

Upvotes: 0

Views: 86

Answers (1)

Himanshu Moradiya
Himanshu Moradiya

Reputation: 4815

Declare numLinesInTextView variable

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
    NSLog(@"%@",text);
    if ([text isEqualToString:@"\n"]) {
          NSLog(@"New line");
    }else{
        int numLines = [textView intrinsicContentSize].height / textView.font.lineHeight;
        NSLog(@"%f",textView.font.lineHeight);

        numLinesInTextView = numLines;
        if (numLinesInTextView == 0) {
            [self.commentTextView setFrame:CGRectMake(60, 10,self.frame.size.width - 130, 30)];
            UIViewController *controller=(UIViewController *)self.delegate;
            self.frame =  CGRectMake(0,controller.view.frame.size.height - 50, controller.view.frame.size.width,50);
            height = 50;
            numLinesInTextView = 0;
        }

    }
    return YES;
}

Add this method in your project and check it its working completely fine. now.

Happy Coding.

Upvotes: 1

Related Questions