CoreTextView可滚动

时间:2013-05-08 20:54:59

标签: uiview core-text scrollable

我正在使用CoreTextView将一些html格式的文本呈现到自定义视图中。我正在尝试学习它是如何工作的(我是iOS的新手),所以我正在玩提供的示例(默认情况下不滚动)并且我设法为内容绘制容器矩形(现在正在红色bg上突出显示它)。 我的问题是我不知道如何使它可滚动。我知道如何在故事板中制作可滚动的视图,但我正在尝试以编程方式完成所有操作,现在我运气不好..这是生成矩形的代码:

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 个答案:

答案 0 :(得分: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];