iPhone文本切頁(yè)代碼案例實(shí)現(xiàn)
作者:gagaga
iPhone文本切頁(yè)代碼案例實(shí)現(xiàn)是本文要介紹的內(nèi)容,主要是來(lái)學(xué)習(xí)文本是如何來(lái)切頁(yè)的,本文主要是基于代碼實(shí)現(xiàn)內(nèi)容,來(lái)看具體實(shí)現(xiàn)代碼。
iPhone文本切頁(yè)代碼案例實(shí)現(xiàn)是本文要介紹的內(nèi)容,主要是來(lái)學(xué)習(xí)文本是如何來(lái)切頁(yè)的,本文主要是基于代碼實(shí)現(xiàn)內(nèi)容,來(lái)看具體實(shí)現(xiàn)代碼。
- //!如果不是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];
- }
小結(jié):iPhone文本切頁(yè)代碼案例實(shí)現(xiàn)的內(nèi)容介紹完了,希望通過(guò)本文的學(xué)習(xí)能對(duì)你有所幫助。
責(zé)任編輯:zhaolei
來(lái)源:
cocoachina