2011-01-14 8 views
1

人々は頻繁にやりたいと思うので、これは簡単なことだと思いますが、私は検索してさまざまなアプローチを試みましたが、何も動作していないようです。UITextView autoSize

私がしたいのは、Textの2行のUITextViewを作成することです。

テキストが長すぎて3行になる場合は、2行に収まるまでフォントを自動サイズ設定します。

概念的には、(テキストフィールドの高さに基づいて)2行に収まるまでテキストを縮小し続ける再帰関数を実行することを計画していましたが、私はそれを取り除くことができませんでした。

ご意見をいただければ幸いです。

+0

UILabelは編集可能である必要はありますか? –

+0

テキストは編集できません。私は基本的に2行のUILabelを探しています。 –

答えて

2

場合、私はここで、このための答えを見つけました:

http://www.11pixel.com/blog/28/resize-multi-line-text-to-fit-uilabel-on-iphone/

//Create a string with the text we want to display. 
self.ourText = @"This is your variable-length string. Assign it any way you want!"; 

/* This is where we define the ideal font that the Label wants to use. 
    Use the font you want to use and the largest font size you want to use. */ 
UIFont *font = [UIFont fontWithName:@"Marker Felt" size:28]; 

int i; 
/* Time to calculate the needed font size. 
    This for loop starts at the largest font size, and decreases by two point sizes (i=i-2) 
    Until it either hits a size that will fit or hits the minimum size we want to allow (i > 10) */ 
for(i = 28; i > 10; i=i-2) 
{ 
    // Set the new font size. 
    font = [font fontWithSize:i]; 
    // You can log the size you're trying: NSLog(@"Trying size: %u", i); 

    /* This step is important: We make a constraint box 
     using only the fixed WIDTH of the UILabel. The height will 
     be checked later. */ 
    CGSize constraintSize = CGSizeMake(260.0f, MAXFLOAT); 

    // This step checks how tall the label would be with the desired font. 
    CGSize labelSize = [self.ourText sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

    /* Here is where you use the height requirement! 
     Set the value in the if statement to the height of your UILabel 
     If the label fits into your required height, it will break the loop 
     and use that font size. */ 
    if(labelSize.height <= 180.0f) 
     break; 
} 
// You can see what size the function is using by outputting: NSLog(@"Best size is: %u", i); 

// Set the UILabel's font to the newly adjusted font. 
msg.font = font; 

// Put the text into the UILabel outlet variable. 
msg.text = self.ourText; 
3

はUILabelを使用して、次のプロパティを設定します。他の誰がこの問題に遭遇

 
adjustsFontSizeToFitWidth = YES; 
minimumFontSize = [UIFont systemFontofSize: 10]; // or whatever suits your app 
numberOfLines = 2; 
+0

動作していません。私はあなたがテキストのようなものが複数ある場合、あなたが自動サイズ設定することはできないと思います。 –

0

をIこの質問のためのビットが遅いと思うが、これはより適切なこのクエリのためのGoogleのトップ結果になるので(コピー&貼り付け)多分25上記のコードで

}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView{ 

if (textView.contentSize.height > textView.frame.size.height) { 
    int fontIncrement = 1; 
    while (textView.contentSize.height > textView.frame.size.height) { 
     textView.font = [UIFont fontWithName:@"Copperplate" size:25.0 - fontIncrement]; 
     fontIncrement++; 

    } 

} 
else { 
    int fontIncrement = 1; 
    while (textView.font.pointSize < 25.0) { 
     textView.font = [UIFont fontWithName:@"Copperplate" size:8.0 + fontIncrement]; 
     fontIncrement++; 

    } 
} 
return YES; 

次のソリューションを使用すると、TextViewのために設定したいthaのmaxFontSize変数です。

p.s. (BOOL)textViewShouldEndEditing:編集が完了してキーボードが辞められる直前に呼び出されたuitextviewのデリゲートメソッドの1つで、ビューコントローラにdelegate protolも適切に含める必要があります。