1

我试图根据内容构建支持可变大小文本框的应用程序。 UITextView的大小应增加到4行,然后激活滚动。获取UItextView中的行数

当输入文本时,我无法获得行数。

请给我一些方法来做到这一点。任何示例代码片段将受到高度赞赏。

在此先感谢!

+0

的可能的复制[如何找到行UITextView的数量(https://stackoverflow.com/questions/7320361/how-to-find-uitextview-number-of-lines) – iWheelBuy 2017-12-27 07:34:47

回答

3

我试过这个问题的一个临时解决方案。您可以在您的视图控制器的 - (void)textViewDidChange:(UITextView *)textView方法中添加以下代码片段,并将代理人更改为IB中的文件所有者。

float adjustvar; 
    if ([textView.text isEqualToString:@""]) { 
     //change these values according to your requirement as they are hard coded to adjust cursor and size of the textview 
     adjustvar = 37.0; 
     textView.contentInset = UIEdgeInsetsMake(6 ,0 ,0 ,0); 
    } 
    else { 
     adjustvar = textView.contentSize.height; 
    } 

    CGRect temp = textView.frame; 
    temp.origin.y = textView.frame.origin.y + textView.frame.size.height - adjustvar; 
    temp.size.height = adjustvar; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.4]; 
    [UIView setAnimationsEnabled:YES]; 

    if (temp.size.height > 142.0) { 
     textView.scrollEnabled = YES; 
    } 
    else { 
     textView.scrollEnabled = NO; 
     textView.frame = temp; 
    } 

    [UIView commitAnimations];` 

希望这会有所帮助。如果有更好的解决方案,请提供帮助。

感谢