iPhone文本切页代码案例实现是本文要介绍的内容,主要是来学习文本是如何来切页的,本文主要是基于代码实现内容,来看具体实现代码。
- //!如果不是HTML,可以把</p>改为n或者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=[@"中" 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 paragraphSplit){
- 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];
- }
小结:iPhone文本切页代码案例实现的内容介绍完了,希望通过本文的学习能对你有所帮助。