Case study of iPhone text page cutting code

Source: Internet
Author: User

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.

 
 
  1. //! If it is not HTML, you can change </p> to n or rn.
  2. -(NSString *) doPagination :( NSString *) pageContent pageNumber :( NSInteger) number
  3. FontSize :( NSInteger) size lineHeight :( NSInteger) lheight pageHeight :( NSInteger) pheight
  4. PageWidth :( NSInteger) pwidth {
  5. UIFont * tfont = [UIFont fontWithName: @ "Helvetica" size: size];
  6. CGFloat singleHeight = [@ "medium" sizeWithFont: tfont constrainedToSize: CGSizeMake (float) pwidth, 9999.0)
  7. LineBreakMode: UILineBreakModeCharacterWrap]. height;
  8. NSInteger maxline_per_page = (int) (pheight/singleHeight );
  9. NSArray * paragraphSplit = [pageContent componentsSeparatedByString: @ "</p>"];
  10. NSString * breakString = @ "nn ";
  11. NSString * contentTrim;
  12. NSInteger totalLines = 0;
  13. NSMutableString * tempString = [NSMutableString string];
  14. NSString * leftString;
  15. CGSize csize;
  16. NSMutableArray * bookPages = [NSMutableArray array];
  17. For (NSString * para in paragrapheatmap ){
  18. ContentTrim = [para stringByReplacingOccurrencesOfString: @ "<p>" withString: breakString];
  19. Csize = [contentTrim sizeWithFont: tfont constrainedToSize: CGSizeMake (float) pwidth, MAXFLOAT)
  20. LineBreakMode: UILineBreakModeCharacterWrap];
  21. TotalLines + = (int) (csize. height/singleHeight );
  22. LeftString = contentTrim;
  23. If (totalLines> maxline_per_page ){
  24. TotalLines-= (int) (csize. height/singleHeight );
  25. For (int I = 0; I <[contentTrim length]; I ++ ){
  26. NSString * calcString = [contentTrim substringWithRange: NSMakeRange (0, I)];
  27. Csize = [calcString sizeWithFont: tfont constrainedToSize: CGSizeMake (float) pwidth, MAXFLOAT)
  28. LineBreakMode: UILineBreakModeCharacterWrap];
  29. TotalLinestotalLines = totalLines + (int) (csize. height/singleHeight );
  30. If (totalLines> maxline_per_page)
  31. {
  32. I --;
  33. If ([calcString length]> 0) [tempString appendString: [calcString substringToIndex :( [calcString length]-1)];
  34. If (I> 0) leftString = [contentTrim substringFromIndex: I]; else leftString = @"";
  35. [BookPages addObject: [[tempString copy] autorelease];
  36. TotalLines = (int) ([leftString sizeWithFont: tfont constrainedToSize: CGSizeMake (float) pwidth, MAXFLOAT)
  37. LineBreakMode: UILineBreakModeCharacterWrap]. height/singleHeight );
  38. [TempString setString: @ ""];
  39. Break;
  40. }
  41. Else totalLinestotalLines = totalLines-(int) (csize. height/singleHeight );
  42. }
  43. }
  44. [TempString appendString: leftString];
  45. }
  46. [BookPages addObject: [[tempString copy] autorelease];
  47. Self. bookContent = [NSArray arrayWithArray: bookPages];
  48. Return [bookPages objectAtIndex: number];
  49. }

Summary:IPhone textBenPage CuttingThe implementation of the code case is complete. I hope this article will help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.