获取 UItextView 中的行数

发布于 2024-10-16 08:21:14 字数 144 浏览 4 评论 0原文

我正在尝试构建一个支持根据内容可变大小文本框的应用程序。 UITextView 的大小应增加最多 4 行,然后激活滚动。

输入文本时我无法获取行数。

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

提前致谢!!

I am trying to build an app that supports variable size text box depending on the content. The size of the UITextView should increase up to 4 lines and then activate the scrolling.

I am not able to get the number of lines when the text is being entered.

Please suggest me some way to do that. Any sample code snippet would be highly appreciated.

Thanks in advance!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小墨 2024-10-23 08:21:14

我尝试了一种临时解决方案来解决这个问题。可以在视图控制器的 - (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];`

希望这有帮助。如果有更好的解决方案请帮忙。

谢谢

I tried one temporary solution to this problem. One can add the following piece of code in - (void)textViewDidChange:(UITextView *)textView method of your view controller and change the delegate to the file owner in 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];`

Hope this helps. If any better solutions kindly help out.

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文