Uiview CoreTextView可滚动

Uiview CoreTextView可滚动,uiview,core-text,scrollable,Uiview,Core Text,Scrollable,我正在使用将一些html格式的文本呈现到自定义视图中。我正在尝试学习它是如何工作的(我是iOS的新手),所以我正在使用提供的示例(默认情况下不会滚动),并且我已经设法为内容绘制了一个容器矩形(现在在红色背景上高亮显示)。 我的问题是我不知道如何使它可滚动。我知道如何在故事板中创建可滚动视图,但我正在尝试以编程方式完成所有操作,而我现在运气不佳。。以下是生成矩形的代码: CGRect screenRect = [[UIScreen mainScreen] bounds]; CGFloat scre

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

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);

您需要创建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];