获取UItextView中的行数

问题描述 投票:1回答:1

我正在尝试构建一个支持可变大小文本框的应用程序,具体取决于内容。 UITextView的大小应该增加到4行,然后激活滚动。

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

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

提前致谢!!

iphone-sdk-3.0 uiscrollview uitextview expandable uitextviewdelegate
1个回答
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];`

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

谢谢

© www.soinside.com 2019 - 2024. All rights reserved.