2
textViewBusiness = [[UITextView alloc] initWithFrame:CGRectMake(25,332,268,60)];
textViewBusiness.text=strMyBusiness;
textViewBusiness.editable=NO;
textViewBusiness.font = [UIFont fontWithName:@"Arial" size: 17.0];
textViewBusiness.layer.borderWidth = 2.0f;
textViewBusiness.layer.borderColor = [[UIColor grayColor] CGColor];
textViewBusiness.backgroundColor = [UIColor clearColor];
[textViewBusiness setTextColor:[UIColor blackColor]];
[self.scrollView addSubview: textViewBusiness];

CGRect frame = textViewBusiness.frame;
frame.size.height = textViewBusiness.contentSize.height;
textViewBusiness.frame = frame;

コンテンツの増加に伴い、テキストフィールドのサイズを大きくしたい....

このコードは私には機能しません...

ありがとう

4

3 に答える 3

2

UITextView編集不可能なテキストを表示する必要はありません。これに加えて、UILabel を使用して、実行時にラベルの高さを調べることができます。コンテンツは、それに応じてフレームのサイズを設定するたびに異なりますUILabel

これを使用して、実行時にラベルの高さを確認します

- (CGFloat) heightOfTextLabel:(NSString *) contentText
{
    CGSize constraint = CGSizeMake(268,4000);

    CGSize size = [contentText sizeWithFont:[UIFont fontWithName:@"Arial" size: 17.0] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

     return size.height;       
}

このメソッドは、コンテンツの可変高さを毎回返します。

この高さをあなたのUILabel

CGFloat heightOfLabel = [self heightOfTextLabel:strMyBusiness];

UILabel* textToShowLabel = [[UILabel alloc] initWithFrame:CGRectMake(25,332,268,heightOfLabel)];

textToShowLabel.text=strMyBusiness;

textToShowLabel.font = [UIFont fontWithName:@"Arial" size: 17.0];

textToShowLabel.layer.borderWidth = 2.0f;

textToShowLabel.layer.borderColor = [[UIColor grayColor] CGColor];

textToShowLabel.backgroundColor = [UIColor clearColor];

[textToShowLabel setTextColor:[UIColor blackColor]];

[self.scrollView addSubview: textToShowLabel];
于 2012-07-16T13:03:57.893 に答える
0

GrowingTextViewは、必要なことを正確に実行する再利用可能な iOS コンポーネントです。ここの GitHub で見つけることができます。

これが役立つことを願っています。

于 2012-07-16T12:27:59.040 に答える
0

コードを 2 つのフラグメントに分割し、適切な場所に配置すると、コードが機能するはずです。

フラグメント 1 (私のテストでは、このフラグメントを viewDidLoad に配置します):

textViewBusiness = [[UITextView alloc] initWithFrame:CGRectMake(25,332,268,60)];
textViewBusiness.text=strMyBusiness;
textViewBusiness.editable=NO;
textViewBusiness.font = [UIFont fontWithName:@"Arial" size: 17.0];
textViewBusiness.layer.borderWidth = 2.0f;
textViewBusiness.layer.borderColor = [[UIColor grayColor] CGColor];
textViewBusiness.backgroundColor = [UIColor clearColor];
[textViewBusiness setTextColor:[UIColor blackColor]];
[self.scrollView addSubview: textViewBusiness];

上記のフラグメントが実行され、テキスト ビューが画面に表示されていることを確認してから、2 番目のフラグメントを実行します。テキスト ビューが画面に表示されない場合、contentView は初期化されておらず、高さは未定義です。

フラグメント 2 (私のテストでは、このフラグメントを に配置しますviewDidAppear):

CGRect frame = textViewBusiness.frame;
frame.size.height = textViewBusiness.contentSize.height;
textViewBusiness.frame = frame;

幸運を!

于 2012-07-16T12:28:09.337 に答える