Reputation: 160
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.
-(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;
}
}
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
2) With single line
How can I resolve it ?Any one have any idea?
Upvotes: 0
Views: 86
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