如何使用分页在多页文本中移动

发布于 2024-08-04 21:25:01 字数 1877 浏览 2 评论 0原文

我正在开发一个视图,该视图允许我显示大量文本,并通过一次显示一个页面并允许用户左右滚动来移动文本。

我想我可以通过将 UITextView 放入 UIScrollView 中来实现,该 UIScrollView 的 pagingEnabled 属性设置为 YES,并将内容宽度设置为屏幕的倍数。

尽管我可以向右滑动并看到其他空白页,但我看到的只是第一页。任何人都可以给出任何提示。这是代码...

label = [[UITextView alloc] initWithFrame: scroller.frame];

label.editable = NO;

label.scrollEnabled = NO;

label.multipleTouchEnabled = NO;

label.userInteractionEnabled = NO;

label.font = [UIFont systemFontOfSize: 14.0];

// Get text 
std::string str;

// Next line gets a single string, which is from 2000 to 8000

//characters long
str = GetStringContent();

NSString * nsStr = [NSString stringWithCString: str.c_str()];

// Measure text that will display in one page.
CGRect rect = CGRectMake(0.0, 0.0, scroller.frame.size.width, scroller.frame.size.height);
CGSize calcSize = [nsStr sizeWithFont: [UIFont systemFontOfSize: 14.0] 
constrainedToSize: rect.size lineBreakMode: UILineBreakModeWordWrap];
rect.size = calcSize;

CGRect maxRect = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 99999.0);

CGSize maxCalcSize = [nsStr sizeWithFont: [UIFont systemFontOfSize: 14.0] 

constrainedToSize: maxRect.size lineBreakMode: UILineBreakModeWordWrap];

float pages = maxCalcSize.height / self.view.bounds.size.height;

int numberOfPages = (int)ceil(pages);

// Set text
label.text = [NSString stringWithCString: str.c_str()];

// Setup scroller
scroller.scrollEnabled = YES;

scroller.pagingEnabled = YES;

scroller.bounces = YES;


scroller.contentSize = CGSizeMake(label.frame.size.width * numberOfPages, scroller.frame.size.height);


scroller.contentOffset = CGPointMake(0, 0); 

scroller.frame = rect;

scroller.clipsToBounds = YES;

[scroller addSubview: label];

[label sizeToFit];

视图是在IB中创建的,并且UIScrollView被添加到IB中,“scroller”是出口的名称。

我已经为此工作了一段时间了,感觉我错过了一些关于 UIScrollView 的基本知识,因为我似乎无法让它工作。

保罗

I am working on a view that would allow me to display large amounts of text, and to move through it by showing a page at a time and allowing the user to scroll right and left.

I thought I could do it by putting a UITextView inside a UIScrollView which has the pagingEnabled property set to YES along with setting the content width to a multiple of the screen.

All I see is the first page, although I can flick to the right and see other blank pages. Can anybody give any hints. Here is the code...

label = [[UITextView alloc] initWithFrame: scroller.frame];

label.editable = NO;

label.scrollEnabled = NO;

label.multipleTouchEnabled = NO;

label.userInteractionEnabled = NO;

label.font = [UIFont systemFontOfSize: 14.0];

// Get text 
std::string str;

// Next line gets a single string, which is from 2000 to 8000

//characters long
str = GetStringContent();

NSString * nsStr = [NSString stringWithCString: str.c_str()];

// Measure text that will display in one page.
CGRect rect = CGRectMake(0.0, 0.0, scroller.frame.size.width, scroller.frame.size.height);
CGSize calcSize = [nsStr sizeWithFont: [UIFont systemFontOfSize: 14.0] 
constrainedToSize: rect.size lineBreakMode: UILineBreakModeWordWrap];
rect.size = calcSize;

CGRect maxRect = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 99999.0);

CGSize maxCalcSize = [nsStr sizeWithFont: [UIFont systemFontOfSize: 14.0] 

constrainedToSize: maxRect.size lineBreakMode: UILineBreakModeWordWrap];

float pages = maxCalcSize.height / self.view.bounds.size.height;

int numberOfPages = (int)ceil(pages);

// Set text
label.text = [NSString stringWithCString: str.c_str()];

// Setup scroller
scroller.scrollEnabled = YES;

scroller.pagingEnabled = YES;

scroller.bounces = YES;


scroller.contentSize = CGSizeMake(label.frame.size.width * numberOfPages, scroller.frame.size.height);


scroller.contentOffset = CGPointMake(0, 0); 

scroller.frame = rect;

scroller.clipsToBounds = YES;

[scroller addSubview: label];

[label sizeToFit];

The view is created in IB, and the UIScrollView was added into IB with "scroller" being the name of the outlet.

I've been working on this for a while, and feel like I am missing something fundamental about UIScrollView, because I can't seem to get this working.

Paul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心碎无痕… 2024-08-11 21:25:01

分页只是意味着滚动视图将“捕捉”到滚动视图的宽度。这意味着当您向左滑动时,下一页将会出现,但会停在 320 的增量处,无论您的宽度如何。

要做你想做的事情将需要比你拥有的更多的代码。我首先制作 3 个文本视图并将它们保存在一个数组中,不过一次只能显示 1 个。

您将需要实现逻辑来确定在这些文本视图上显示什么(显示文本的哪一部分),并使用此逻辑来填充当前页面、上一页和下一页。

您将需要监听 uiscrollview 委托方法来了解您所在的页面,并且您将需要在用户滚动时重新计算并填充页面

如果用户滚动到下一页,您将获取 2 页的文本视图返回,并将其移动到新的“下一页”。这称为“回收”您的视图,并且有关于此的大量问题与示例代码有关

Paging just means that the scrollview will "snap" to the width of the scroll view. Meaning that as you swipe left, the next page will come but stop at the increment of 320, ow whatever width you have it.

To do what you wish to do will require a little bit more code than you have. I would start by making a 3 text views and keeping them in an array, you only ever show 1 at a time though.

You will need to implement logic to figure out what to display on these textviews (which part of the text to show) and use this logic to fill the current page, the previous page, and the next page.

You will need to listen to the uiscrollview delegate methods to know which page you are on, and you will need to recalculate and fill the pages as the user scrolls

If the user scrolls to the next page, you take the textview you had for 2 pages back, and move it to become the new "next" page. This is called "recycling" your views and there are plenty of questions on SO regarding this with sample code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文