1

コンテンツに応じて可変サイズのテキストボックスをサポートするアプリを作成しようとしています。UITextViewのサイズを最大4行増やしてから、スクロールをアクティブにする必要があります。

テキストを入力しているときに行数を取得できません。

その方法を教えてください。サンプルコードスニペットをいただければ幸いです。

前もって感謝します!!

4

1 に答える 1

3

この問題に対する一時的な解決策を 1 つ試しました。次のコードを(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];`

お役に立てれば。より良い解決策があれば、親切に助けてください。

ありがとう

于 2011-02-09T08:13:38.553 に答える