开发者

iPhone: text split over multiple pages(page control + text view)

开发者 https://www.devze.com 2023-04-11 20:18 出处:网络
I am currently working on an iphone app开发者_运维问答 and I want to display a large amount of text over multiple pages, but I can\'t think of a good or effective way of doing it.

I am currently working on an iphone app开发者_运维问答 and I want to display a large amount of text over multiple pages, but I can't think of a good or effective way of doing it.

I am currently using a character count to determine how many pages to use and which portion of text is loaded into that page, but it fails in some cases, because the logic is not great. I have listed some of my code below.

Does anyone have any suggestions, like is maybe trying to count words, instead of characters, a better approach?

Determining how many pages are needed:

//Character count used is 740
int iCharactersPerPage = 740;
int iNumberOfPages = (self.passFunctionalityName.length/iCharactersPerPage);
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < iNumberOfPages ; i++) 
{
    [controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];

Assigning text to a page:

//Get the text depending on the page number
NSString *text;
NSLog(@"%d", pageNumber);
NSLog(@"%d", totalNumberOfPages);

if((totalNumberOfPages-1) == pageNumber)
{
    text = [passFunctionalityName substringWithRange: NSMakeRange(740*pageNumber, passFunctionalityName.length-740*pageNumber)]; 
    self.textView.text = text;
}
else
{

    text = [passFunctionalityName substringWithRange: NSMakeRange(740*pageNumber, 740)]; 
    self.textView.text = text;
}

Thank you for your time and help. If you have any questions or need me to explain more please let me know.

Jing Jing Tao


One thing you can do is temporarily put the text into a UILabel and determine the frame size. Then determine the number of pages needed. I use the following code to determine dynamic frame sizes to layout view hierarchy with multiple labels when the size of the label is not know until run time with this code. Note in this code the frame information of the previous label is used to position this label.

UILabel *titleLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.lineBreakMode = UILineBreakModeWordWrap;
titleLabel.numberOfLines = 0;
titleLabel.font = [UIFont systemFontOfSize:22];
titleLabel.textColor = [UIColor blackColor];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.text = [managedObject valueForKey:@"title"];
[titleLabel setUpMultiLineFrameBasedOnWidth:310.0 withStartXPosition:5.0 withStartYPosition:(frame.origin.y + frame.size.height + 5.0)];

Then access the frame size to compute the location of the next component on the view

frame = titleLabel.frame;

You could probably do something similar to compute height for a given amount of text to paginate.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号