2

我正在使用CoreTextView将一些 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);
4

1 回答 1

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];
于 2013-05-09T16:29:29.530 回答