2013-05-08 37 views
2

我正在使用CoreTextView將某些html格式的文本呈現爲自定義視圖。我試圖學習它是如何工作的(而且我是iOS的新手),所以我玩弄了提供的示例(默認情況下不滾動),並且設法爲內容繪製容器矩形(現在在紅色的bg上突出顯示它)。 我的問題是,我不知道如何讓它滾動。我知道如何在故事板滾動的意見,但我想以編程方式做的一切,而我現在的運氣..下面是生成矩形的代碼:CoreTextView可滾動

CGRect screenRect = [[UIScreen mainScreen] bounds]; 
CGFloat screenWidth = screenRect.size.width; 
CGFloat screenHeight = screenRect.size.height; 

int widthInt = (int)roundf(screenWidth)-20; 
int heightInt= (int)roundf(screenHeight)-60; 
m_coreText=[[CoreTextView alloc] initWithFrame:CGRectMake(10, 50, widthInt, heightInt)]; 
m_coreText.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; 
m_coreText.backgroundColor=[UIColor redColor]; 
m_coreText.contentInset=UIEdgeInsetsMake(10, 10, 10, 10); 

回答

1

你需要創建一個UIScrollView包裝並設置適當的contentSize:

m_coreText.frame=CGRectMake(0, 0, self.view.bounds.size.width, [m_coreText sizeThatFits:CGSizeMake(self.view.bounds.size.width, MAXFLOAT)].height); 

UIScrollView* scroll=[[UIScrollView alloc] initWithFrame:self.view.bounds]; 
scroll.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; 
scroll.contentSize=m_coreText.frame.size; 
[scroll addSubview:m_coreText]; 
[self.view addSubview:scroll];