The implementation of the iPhone text page-cutting code case is the content to be introduced in this article, mainly to learn how the text is used for page-cutting. This article is mainly based on the Code implementation content to see the specific implementation code.
- //! If it is not HTML, you can change </p> to n or rn.
- -(NSString *) doPagination :( NSString *) pageContent pageNumber :( NSInteger) number
- FontSize :( NSInteger) size lineHeight :( NSInteger) lheight pageHeight :( NSInteger) pheight
- PageWidth :( NSInteger) pwidth {
- UIFont * tfont = [UIFont fontWithName: @ "Helvetica" size: size];
- CGFloat singleHeight = [@ "medium" sizeWithFont: tfont constrainedToSize: CGSizeMake (float) pwidth, 9999.0)
- LineBreakMode: UILineBreakModeCharacterWrap]. height;
- NSInteger maxline_per_page = (int) (pheight/singleHeight );
- NSArray * paragraphSplit = [pageContent componentsSeparatedByString: @ "</p>"];
- NSString * breakString = @ "nn ";
- NSString * contentTrim;
- NSInteger totalLines = 0;
- NSMutableString * tempString = [NSMutableString string];
- NSString * leftString;
- CGSize csize;
- NSMutableArray * bookPages = [NSMutableArray array];
- For (NSString * para in paragrapheatmap ){
- ContentTrim = [para stringByReplacingOccurrencesOfString: @ "<p>" withString: breakString];
- Csize = [contentTrim sizeWithFont: tfont constrainedToSize: CGSizeMake (float) pwidth, MAXFLOAT)
- LineBreakMode: UILineBreakModeCharacterWrap];
- TotalLines + = (int) (csize. height/singleHeight );
- LeftString = contentTrim;
- If (totalLines> maxline_per_page ){
- TotalLines-= (int) (csize. height/singleHeight );
- For (int I = 0; I <[contentTrim length]; I ++ ){
- NSString * calcString = [contentTrim substringWithRange: NSMakeRange (0, I)];
- Csize = [calcString sizeWithFont: tfont constrainedToSize: CGSizeMake (float) pwidth, MAXFLOAT)
- LineBreakMode: UILineBreakModeCharacterWrap];
- TotalLinestotalLines = totalLines + (int) (csize. height/singleHeight );
- If (totalLines> maxline_per_page)
- {
- I --;
- If ([calcString length]> 0) [tempString appendString: [calcString substringToIndex :( [calcString length]-1)];
- If (I> 0) leftString = [contentTrim substringFromIndex: I]; else leftString = @"";
- [BookPages addObject: [[tempString copy] autorelease];
- TotalLines = (int) ([leftString sizeWithFont: tfont constrainedToSize: CGSizeMake (float) pwidth, MAXFLOAT)
- LineBreakMode: UILineBreakModeCharacterWrap]. height/singleHeight );
- [TempString setString: @ ""];
- Break;
- }
- Else totalLinestotalLines = totalLines-(int) (csize. height/singleHeight );
- }
- }
- [TempString appendString: leftString];
- }
- [BookPages addObject: [[tempString copy] autorelease];
- Self. bookContent = [NSArray arrayWithArray: bookPages];
- Return [bookPages objectAtIndex: number];
- }
Summary:IPhone textBenPage CuttingThe implementation of the code case is complete. I hope this article will help you.