自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

實現(xiàn)iPhone電子書的分頁顯示功能(附代碼)

移動開發(fā) iOS
制作iPhone電子書時,如果把大段文字放在 UITextView 或 UILabel 里顯示,是不能分頁的,閱讀時就像再看一大卷滾不到頭的紙帶,用戶體驗很差。下面這段代碼可以實現(xiàn) UILabel 尺寸固定,根據(jù)文本內(nèi)容和字體動態(tài)分頁顯示,電子書方面的應用應該非常有用。

最近我們介紹了幾個iPhone開發(fā)的代碼測試案例,今天我們將介紹iPhone電子書的分頁功能的代碼。制作iPhone電子書時,如果把大段文字放在 UITextView 或 UILabel 里顯示,是不能分頁的,閱讀時就像再看一大卷滾不到頭的紙帶,用戶體驗很差。下面這段代碼可以實現(xiàn) UILabel 尺寸固定,根據(jù)文本內(nèi)容和字體動態(tài)分頁顯示,電子書方面的應用應該非常有用。 

  1. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.  
  2. - (void)viewDidLoad {  
  3.     [super viewDidLoad];  
  4.       
  5.     //  
  6.     totalPages = 0;  
  7.     currentPage = 0;  
  8.       
  9.     //  
  10.     textLabel.numberOfLines = 0;  
  11.       
  12.     //  
  13.     if (!text) {  
  14.         // 從文件里加載文本串  
  15.         [self loadString];  
  16.           
  17.         // 計算文本串的大小尺寸  
  18.         CGSize totalTextSize = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE_MAX]  
  19.                                          constrainedToSize:CGSizeMake(textLabel.frame.size.width, CGFLOAT_MAX)  
  20.                                     lineBreakMode:UILineBreakModeWordWrap];  
  21.  
  22.         // 如果一頁就能顯示完,直接顯示所有文本串即可。  
  23.         if (totalTextSize.height < textLabel.frame.size.height) {  
  24.             texttextLabel.text = text;  
  25.         }  
  26.         else {  
  27.             // 計算理想狀態(tài)下的頁面數(shù)量和每頁所顯示的字符數(shù)量,只是拿來作為參考值用而已!  
  28.             NSUInteger textLength = [text length];  
  29.             referTotalPages = (int)totalTextSize.height/(int)textLabel.frame.size.height+1;  
  30.             referCharatersPerPage = textLength/referTotalPages;  
  31.               
  32.             // 申請最終保存頁面NSRange信息的數(shù)組緩沖區(qū)  
  33.             int maxPages = referTotalPages;  
  34.             rangeOfPages = (NSRange *)malloc(referTotalPages*sizeof(NSRange));  
  35.             memset(rangeOfPages, 0x0, referTotalPages*sizeof(NSRange));  
  36.               
  37.             // 頁面索引  
  38.             int page = 0;  
  39.               
  40.             for (NSUInteger location = 0; location < textLength; ) {  
  41.                 // 先計算臨界點(尺寸剛剛超過UILabel尺寸時的文本串)  
  42.                 NSRange range = NSMakeRange(location, referCharatersPerPage);  
  43.                   
  44.                 // reach end of text ?  
  45.                 NSString *pageText;  
  46.                 CGSize pageTextSize;  
  47.                   
  48.                 while (range.location + range.length < textLength) {  
  49.                     pageText = [text substringWithRange:range];  
  50.                       
  51.                     pageTextSize = [pageText sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE_MAX]  
  52.                                         constrainedToSize:CGSizeMake(textLabel.frame.size.width, CGFLOAT_MAX)  
  53.                                             lineBreakMode:UILineBreakModeWordWrap];  
  54.                       
  55.                     if (pageTextSize.height > textLabel.frame.size.height) {  
  56.                         break;  
  57.                     }  
  58.                     else {  
  59.                         range.length += referCharatersPerPage;  
  60.                     }  
  61.                 }  
  62.                   
  63.                 if (range.location + range.length >= textLength) {  
  64.                     range.length = textLength - range.location;  
  65.                 }  
  66.                   
  67.                 // 然后一個個縮短字符串的長度,當縮短后的字符串尺寸小于textLabel的尺寸時即為滿足  
  68.                 while (range.length > 0) {  
  69.                     pageText = [text substringWithRange:range];  
  70.                       
  71.                     pageTextSize = [pageText sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE_MAX]  
  72.                                         constrainedToSize:CGSizeMake(textLabel.frame.size.width, CGFLOAT_MAX)  
  73.                                             lineBreakMode:UILineBreakModeWordWrap];  
  74.                       
  75.                     if (pageTextSize.height <= textLabel.frame.size.height) {  
  76.                         range.length = [pageText length];  
  77.                         break;  
  78.                     }  
  79.                     else {  
  80.                         range.length -2;  
  81.                     }  
  82.                 }  
  83.                   
  84.                 // 得到一個頁面的顯示范圍  
  85.                 if (page >= maxPages) {  
  86.                     maxPages += 10;  
  87.                     rangeOfPages = (NSRange *)realloc(rangeOfPages, maxPages*sizeof(NSRange));  
  88.                 }  
  89.                 rangeOfPages[page++] = range;  
  90.                   
  91.                 // 更新游標  
  92.                 location += range.length;  
  93.             }  
  94.  
  95.             // 獲取最終頁面數(shù)量  
  96.             totalPages = page;  
  97.               
  98.             // 更新UILabel內(nèi)容  
  99.             textLabel.text = [text substringWithRange:rangeOfPages[currentPage]];  
  100.         }  
  101.     }  
  102.       
  103.     // 顯示當前頁面進度信息,格式為:"8/100"  
  104.     pageInfoLabel.text = [NSString stringWithFormat:@"%d/%d", currentPage+1, totalPages];  
  105. }  
  106.  
  107.  
  108. ////////////////////////////////////////////////////////////////////////////////////////  
  109. // 上一頁  
  110. - (IBAction)actionPrevious:(id)sender {  
  111.     if (currentPage > 0) {  
  112.         currentPage--;  
  113.           
  114.         NSRange range = rangeOfPages[currentPage];  
  115.         NSString *pageText = [text substringWithRange:range];  
  116.           
  117.         textLabel.text = pageText;  
  118.  
  119.         //  
  120.         pageInfoLabel.text = [NSString stringWithFormat:@"%d/%d", currentPage+1, totalPages];  
  121.     }  
  122. }  
  123.  
  124. ////////////////////////////////////////////////////////////////////////////////////////  
  125. // 下一頁  
  126. - (IBAction)actionNext:(id)sender {  
  127.     if (currentPage < totalPages-1) {  
  128.         currentPage++;  
  129.           
  130.         NSRange range = rangeOfPages[currentPage];  
  131.         NSString *pageText = [text substringWithRange:range];  
  132.           
  133.         textLabel.text = pageText;  
  134.           
  135.         //  
  136.         pageInfoLabel.text = [NSString stringWithFormat:@"%d/%d", currentPage+1, totalPages];  
  137.     }  
  138. }  

在設(shè)計iPhone電子書時,一定要注意UILabel 尺寸固定,否則是無法實現(xiàn)iPhone電子書的分頁。

[[15730]]

【編輯推薦】

  1. iPhone內(nèi)存管理面面觀 自動釋放與便捷方法
  2. iPhone內(nèi)存管理面面觀 對象所有權(quán)與引用計數(shù)
  3. iPhone開發(fā)入門守則:Objective-C編碼規(guī)范
  4. iPhone開發(fā)入門篇 “Hello World”分析代碼
責任編輯:佚名 來源: cocoachina
相關(guān)推薦

2011-08-19 09:54:40

iPhone開發(fā)電子書字符串

2011-08-08 10:42:46

iPhone UITableVie 分頁

2009-06-12 16:52:49

2013-06-14 10:13:06

PythonPython電子書Python教程

2020-03-03 15:17:45

Linux電子書命令

2011-06-27 13:17:56

Java

2012-03-20 09:43:11

Boogie Boar

2010-07-19 16:57:45

盈動電子書包12大功能全接觸

2011-12-13 14:43:29

51CTO

2020-06-15 18:20:37

Fedora電子書開源

2012-04-13 18:57:22

2018-12-26 10:06:28

Linux電子書閱讀器命令

2009-06-17 16:12:26

java電子書制作軟件

2018-12-05 16:00:32

MongoDB數(shù)據(jù)庫NoSQL

2017-01-19 19:03:25

微軟Edge瀏覽器電子書

2016-11-23 08:48:24

LinuxCalibre電子書

2013-07-11 09:57:43

蘋果電子書

2011-03-28 15:57:03

Python

2023-11-19 18:58:12

LinuxFoliate

2019-12-16 11:12:48

開源技術(shù) 數(shù)據(jù)
點贊
收藏

51CTO技術(shù)棧公眾號