我正在尝试将相当长的字符串的内容拆分为内容页面。现在我按字符(每页 500 个字符)执行此操作,如下所示:
//Lets find out how many pages to make
int pageLength = 500; //how many characters per page
NSString *text = ((Story *) [self.story objectAtIndex:chapter]).content;
int NumberOfPages = (text.length/pageLength);
//NumberOfPages += 1;
//Build the Pages Array
NSMutableArray *pageStrings = [[NSMutableArray alloc] init];
for (int i = 0; i <= (NumberOfPages+1); i++)
{
if (i < NumberOfPages) {
//Load the text like normal
NSString *contentString = [[NSString alloc]initWithFormat:@"<html><head><style type=text/css>body {font-family: \"%@\"; font-size: %d;}</style></head><body><p>%@</p></body></htlm>",@"helvetica",20,[text substringWithRange:NSMakeRange(i*pageLength,pageLength)]];
[pageStrings addObject:contentString];
}
if (i == NumberOfPages) {
//on the last page, only load what's available
NSString *contentString = [[NSString alloc]initWithFormat:@"<html><head><style type=text/css>body {font-family: \"%@\"; font-size: %d;}</style></head><body><p>%@</p></body></htlm>",@"helvetica",20,[text substringWithRange:NSMakeRange(i*pageLength,(text.length-(i*pageLength)))]];
[pageStrings addObject:contentString];
}
if (i > NumberOfPages){
//add in a blank page on the end
NSString *contentString = [[NSString alloc]initWithFormat:@"<html><head><style type=text/css>body {font-family: \"%@\"; font-size: %d;}</style></head><body><p>%@</p></body></htlm>",@"helvetica",20,@"What do you do?"];
[pageStrings addObject:contentString];
}
}
pageContent = [[NSArray alloc] initWithArray:pageStrings];
这很好用,但通常会导致单词在中间分开。我正试图让它在这个词上分裂。该字符串不必正好是 500 个字符,只要接近它即可。