2012-01-18 5 views
0

Scrollview内にTextViewを挿入しようとしています。スクロールビューの動作はしますが、TextViewの内容は完全に表示されません。スクロールが表示されるためです。私はスクロールせずにTextViewの完全な内容を表示します。TextView autoheight in ScrollView

@implementation DetailViewController 
@synthesize scroller; 

-(void) setTextViewForRecipe:(Recipe *)theRecipe 
{ 
[detailTextView setText:theRecipe.detail]; 
} 

- (void)viewDidLoad { 
CGRect frame = detailTextView.frame; 
frame.size.height = detailTextView.contentSize.height; 
detailTextView.frame = frame; 
[scroller setContentSize: CGSizeMake(280, detailTextView.frame.origin.y + detailTextView.frame.size.height + 10)];  
} 

答えて

0

enter image description here

ファイル.h

@interface DetailViewController : UIViewController{ 
IBOutlet UITextView *detailTextView; 
IBOutlet UIScrollView *scroller;  
} 

@property(retain, nonatomic) IBOutlet UIScrollView *scroller; 

-(void)setTextViewForRecipes: (Recipe *)theRecipe; 

@end 

file.mはUITextViewはUIScrollViewのサブクラスであるので、あなたは、スクロールビューに追加するべきではありません。

0

bandejapaisa pointed outとして、UITextViewUIScrollViewにラップする必要はありません。これは、textViewが単独でスクロールできるためです。

しかし、あなたは本当にこれが必要見つけ、場合それは、特定のフォントでレンダリングされた場合は、テキストのサイズを見つけることができます。

CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:fontSizeOfYourTextView] 
        constrainedToSize:CGSizeMake(widthOfYourTextView, MAXFLOAT) 
         lineBreakMode:UILineBreakModeOfYourTextView]; 

これは、高さを見つけるでしょう。あなたのニーズに適応しますが、警告を受けてください:これは少しハッキリします。希望の結果を得るには、試行錯誤を繰り返す必要があります。

2

detailTextViewのフレームの高さをcontentSizeの高さに設定することで、viewDidLoadに適切なアイデアが得られました。しかし、あなたはビューのテキストを設定した後でそれを行う必要があります。もちろん、スクロール側のcontentSizeを再度調整する必要があります。

-(void) setTextViewForRecipe:(Recipe *)theRecipe 
{ 
    detailTextView.text = theRecipe.detail; 
    CGRect frame = detailTextView.frame; 
    frame.size.height = detailTextView.contentSize.height; 
    detailTextView.frame = frame; 
    scroller.contentSize = CGSizeMake(280, 10 + CGRectGetMaxY(frame)); 
}