2012-01-18 27 views
0

我试图在Scrollview中插入TextView。滚动视图的工作,但TextView的内容不完整,因为出现他们的滚动。我会显示完整的TextView内容而不滚动。ScrollView中的TextView自动高亮

enter image description here

file.h

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

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

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

@end 

file.m

@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

的UITextView是UIScrollView的子类,所以你应该把它添加到一个滚动视图。

0

作为bandejapaisa pointed out,通常不需要在UIScrollView中包装UITextView,因为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)); 
}