iPhone文本切页代码案例实现

移动开发 iOS
iPhone文本切页代码案例实现是本文要介绍的内容,主要是来学习文本是如何来切页的,本文主要是基于代码实现内容,来看具体实现代码。

iPhone文本切页代码案例实现是本文要介绍的内容,主要是来学习文本是如何来切页的,本文主要是基于代码实现内容,来看具体实现代码。

  1. //!如果不是HTML,可以把</p>改为n或者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=[@"中" 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 paragraphSplit){  
  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];  

小结:iPhone文切页代码案例实现的内容介绍完了,希望通过本文的学习能对你有所帮助。

责任编辑:zhaolei 来源: cocoachina
相关推荐

2011-08-19 10:05:30

iPhone开发

2011-08-18 16:42:07

iPhone应用APNS推送

2011-08-18 16:03:34

iPhone上传图片

2011-08-15 15:44:46

iPhone开发PDF

2011-08-18 16:24:44

iPhone开发图片

2011-08-09 14:54:29

iPhoneNSDateanotherDate

2011-07-20 15:20:14

IPhone AVAudioRec

2011-08-19 10:13:05

iPhone开发

2011-08-19 11:10:31

iPhone应用

2011-08-16 15:48:37

iPhone开发抓图程序

2011-08-19 10:39:01

iPhone App输入面板

2011-08-19 17:02:46

iPhone开发

2011-08-17 16:12:20

iPhone应用程序

2011-07-27 11:19:33

iPhone UITableVie

2011-08-17 16:23:31

iPhone开发UIViewContr

2011-08-16 15:36:47

iPhone应用测试

2011-08-18 15:24:40

iPhone国际化

2011-08-17 16:29:12

iPhone开发UIButton

2011-08-19 10:01:09

iPhone应用SqliteUITableView

2011-07-20 17:29:12

iPhone 网络
点赞
收藏

51CTO技术栈公众号