Skip to main content
some edits in the code
Source Link
CalZone
  • 1.7k
  • 1
  • 17
  • 29
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
CGFloat height = [textView.text sizeWithFont:textView.font].height;
//CGFloat lineWidth;
        NSLog(@"initial height = %f", self.textViewHeight);
int charsAsInt;
 if ([[NSCharacterSet newlineCharacterSet] characterIsMember:[text characterAtIndex:text.length-1]])
 //if{
 ([[NSCharacterSet newlineCharacterSet] characterIsMember:[text characterAtIndex:range.location -self.lineWidth 1]])
= {0;
     lineWidthnewLine = 0;@"";
     self.textViewHeight += height;
     NSLog(@"height: %f, width: %f, new Height: %f", height, self.textViewWidth, self.textViewHeight);
     [textView setFrame:CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.textViewWidth, self.textViewHeight)];
     self.contentView.frame = textView.frame;
     self.frame = textView.frame;
     [_editButton setFrame:CGRectMake(self.contentView.frame.size.width - 14, 0, 28, 28)];
     //[super setContentView:self.contentView];
     [super setFrame:textView.frame];
 }
 else{
     //CGFloat heightnewLine = [textView[newLine stringByAppendingString:text];
     self.textlineWidth = [newLine sizeWithFont:textView.font].height;width;
     charsAsInt = (int)(textView.textnewLine.length);
     CGFloat charWidth = self.lineWidth / charsAsInt;
    NSLog(@"height: %f, width: %f, number of characters: 
 %f", height, lineWidth, charWidth);
     if(self.lineWidth >= self.textViewWidth){
         self.textViewWidth = self.lineWidth;
         [textView setFrame:CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.lineWidth, self.textViewHeight)];
     }
     
     else{
         [textView setFrame:CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.textViewWidth+ charWidthtextViewWidth, self.textViewHeight)];
     }
     self.contentView.frame = textView.frame;
     self.frame = textView.frame;
     [_editButton setFrame:CGRectMake(self.contentView.frame.size.width - 14, 0, 28, 28)];
     //[super setContentView:self.contentView];
     [super setFrame:textView.frame];
}

return YES;
}

Whats happening is We have a multiline UITextView Sticker, so when you press return on your keyboard, you have a new line. Which I'm storing in a new variable now. And the width of whole textVIew is increasing only if width of new line is more than width of old line(self.textViewWidth)

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
CGFloat height = [textView.text sizeWithFont:textView.font].height;
CGFloat lineWidth;
        NSLog(@"initial height = %f", self.textViewHeight);
int charsAsInt;
 if ([[NSCharacterSet newlineCharacterSet] characterIsMember:[text characterAtIndex:text.length-1]])
 //if ([[NSCharacterSet newlineCharacterSet] characterIsMember:[text characterAtIndex:range.location - 1]])
 {
     lineWidth = 0;
     self.textViewHeight += height;
     NSLog(@"height: %f, width: %f, new Height: %f", height, self.textViewWidth, self.textViewHeight);
     [textView setFrame:CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.textViewWidth, self.textViewHeight)];
     self.contentView.frame = textView.frame;
     self.frame = textView.frame;
     [_editButton setFrame:CGRectMake(self.contentView.frame.size.width - 14, 0, 28, 28)];
     //[super setContentView:self.contentView];
     [super setFrame:textView.frame];
 }
 else{
     //CGFloat height = [textView.text sizeWithFont:textView.font].height;
    charsAsInt = (int)(textView.text.length);
    CGFloat charWidth = lineWidth / charsAsInt;
    NSLog(@"height: %f, width: %f, number of characters: %f", height, lineWidth, charWidth);
     if(lineWidth >= self.textViewWidth){
         self.textViewWidth = lineWidth;
     }
    [textView setFrame:CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.textViewWidth+ charWidth, self.textViewHeight)];
     self.contentView.frame = textView.frame;
     self.frame = textView.frame;
     [_editButton setFrame:CGRectMake(self.contentView.frame.size.width - 14, 0, 28, 28)];
     //[super setContentView:self.contentView];
     [super setFrame:textView.frame];
}

return YES;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
CGFloat height = [textView.text sizeWithFont:textView.font].height;
//CGFloat lineWidth;

int charsAsInt;
 if ([[NSCharacterSet newlineCharacterSet] characterIsMember:[text characterAtIndex:text.length-1]])
 {
     self.lineWidth = 0;
     newLine = @"";
     self.textViewHeight += height;
     NSLog(@"height: %f, width: %f, new Height: %f", height, self.textViewWidth, self.textViewHeight);
     [textView setFrame:CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.textViewWidth, self.textViewHeight)];
     self.contentView.frame = textView.frame;
     self.frame = textView.frame;
     [_editButton setFrame:CGRectMake(self.contentView.frame.size.width - 14, 0, 28, 28)];
     
     [super setFrame:textView.frame];
 }
 else{
     newLine = [newLine stringByAppendingString:text];
     self.lineWidth = [newLine sizeWithFont:textView.font].width;
     charsAsInt = (int)(newLine.length);
     CGFloat charWidth = self.lineWidth / charsAsInt;
      
    
     if(self.lineWidth >= self.textViewWidth){
         self.textViewWidth = self.lineWidth;
         [textView setFrame:CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.lineWidth, self.textViewHeight)];
     }
     
     else{
         [textView setFrame:CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.textViewWidth, self.textViewHeight)];
     }
     self.contentView.frame = textView.frame;
     self.frame = textView.frame;
     [_editButton setFrame:CGRectMake(self.contentView.frame.size.width - 14, 0, 28, 28)];
     //[super setContentView:self.contentView];
     [super setFrame:textView.frame];
}

return YES;
}

Whats happening is We have a multiline UITextView Sticker, so when you press return on your keyboard, you have a new line. Which I'm storing in a new variable now. And the width of whole textVIew is increasing only if width of new line is more than width of old line(self.textViewWidth)

Source Link
CalZone
  • 1.7k
  • 1
  • 17
  • 29

OK the width is width of whole text in all the lines. So have to find a max line length.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
CGFloat height = [textView.text sizeWithFont:textView.font].height;
CGFloat lineWidth;
        NSLog(@"initial height = %f", self.textViewHeight);
int charsAsInt;
 if ([[NSCharacterSet newlineCharacterSet] characterIsMember:[text characterAtIndex:text.length-1]])
 //if ([[NSCharacterSet newlineCharacterSet] characterIsMember:[text characterAtIndex:range.location - 1]])
 {
     lineWidth = 0;
     self.textViewHeight += height;
     NSLog(@"height: %f, width: %f, new Height: %f", height, self.textViewWidth, self.textViewHeight);
     [textView setFrame:CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.textViewWidth, self.textViewHeight)];
     self.contentView.frame = textView.frame;
     self.frame = textView.frame;
     [_editButton setFrame:CGRectMake(self.contentView.frame.size.width - 14, 0, 28, 28)];
     //[super setContentView:self.contentView];
     [super setFrame:textView.frame];
 }
 else{
     //CGFloat height = [textView.text sizeWithFont:textView.font].height;
    charsAsInt = (int)(textView.text.length);
    CGFloat charWidth = lineWidth / charsAsInt;
    NSLog(@"height: %f, width: %f, number of characters: %f", height, lineWidth, charWidth);
     if(lineWidth >= self.textViewWidth){
         self.textViewWidth = lineWidth;
     }
    [textView setFrame:CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.textViewWidth+ charWidth, self.textViewHeight)];
     self.contentView.frame = textView.frame;
     self.frame = textView.frame;
     [_editButton setFrame:CGRectMake(self.contentView.frame.size.width - 14, 0, 28, 28)];
     //[super setContentView:self.contentView];
     [super setFrame:textView.frame];
}

return YES;
}