Sealyu

--- 博客已迁移至: http://www.sealyu.com/blog

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  618 随笔 :: 87 文章 :: 225 评论 :: 0 Trackbacks
制作 iPhone 电子书时,如果把大段文字放在 UITextView 或 UILabel 里显示,是不能分页的,阅读时就像再看一大卷滚不到头的纸带,用户体验很差。下面这段代码可以实现 UILabel 尺寸固定,

  制作 iPhone 电子书时,如果把大段文字放在 UITextView 或 UILabel 里显示,是不能分页的,阅读时就像再看一大卷滚不到头的纸带,用户体验很差。下面这段代码可以实现 UILabel 尺寸固定,根据文本内容和字体动态分页显示,电子书方面的应用应该非常有用。

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //
    totalPages = 0;
    currentPage = 0;
    
    //
    textLabel.numberOfLines = 0;
    
    //
    if (!text) {
        // 从文件里加载文本串
        [self loadString];
        
        // 计算文本串的大小尺寸
        CGSize totalTextSize = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE_MAX]
                                         constrainedToSize:CGSizeMake(textLabel.frame.size.width, CGFLOAT_MAX)
                                    lineBreakMode:UILineBreakModeWordWrap];

        // 如果一页就能显示完,直接显示所有文本串即可。
        if (totalTextSize.height < textLabel.frame.size.height) {
            textLabel.text = text;
        }
        else {
            // 计算理想状态下的页面数量和每页所显示的字符数量,只是拿来作为参考值用而已!
            NSUInteger textLength = [text length];
            referTotalPages = (int)totalTextSize.height/(int)textLabel.frame.size.height+1;
            referCharatersPerPage = textLength/referTotalPages;
            
            // 申请最终保存页面NSRange信息的数组缓冲区
            int maxPages = referTotalPages;
            rangeOfPages = (NSRange *)malloc(referTotalPages*sizeof(NSRange));
            memset(rangeOfPages, 0x0, referTotalPages*sizeof(NSRange));
            
            // 页面索引
            int page = 0;
            
            for (NSUInteger location = 0; location < textLength; ) {
                // 先计算临界点(尺寸刚刚超过UILabel尺寸时的文本串)
                NSRange range = NSMakeRange(location, referCharatersPerPage);
                
                // reach end of text ?
                NSString *pageText;
                CGSize pageTextSize;
                
                while (range.location + range.length < textLength) {
                    pageText = [text substringWithRange:range];
                    
                    pageTextSize = [pageText sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE_MAX]
                                        constrainedToSize:CGSizeMake(textLabel.frame.size.width, CGFLOAT_MAX)
                                            lineBreakMode:UILineBreakModeWordWrap];
                    
                    if (pageTextSize.height > textLabel.frame.size.height) {
                        break;
                    }
                    else {
                        range.length += referCharatersPerPage;
                    }
                }
                
                if (range.location + range.length >= textLength) {
                    range.length = textLength - range.location;
                }
                
                // 然后一个个缩短字符串的长度,当缩短后的字符串尺寸小于textLabel的尺寸时即为满足
                while (range.length > 0) {
                    pageText = [text substringWithRange:range];
                    
                    pageTextSize = [pageText sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE_MAX]
                                        constrainedToSize:CGSizeMake(textLabel.frame.size.width, CGFLOAT_MAX)
                                            lineBreakMode:UILineBreakModeWordWrap];
                    
                    if (pageTextSize.height <= textLabel.frame.size.height) {
                        range.length = [pageText length];
                        break;
                    }
                    else {
                        range.length -= 2;
                    }
                }
                
                // 得到一个页面的显示范围
                if (page >= maxPages) {
                    maxPages += 10;
                    rangeOfPages = (NSRange *)realloc(rangeOfPages, maxPages*sizeof(NSRange));
                }
                rangeOfPages[page++] = range;
                
                // 更新游标
                location += range.length;
            }

            // 获取最终页面数量
            totalPages = page;
            
            // 更新UILabel内容
            textLabel.text = [text substringWithRange:rangeOfPages[currentPage]];
        }
    }
    
    // 显示当前页面进度信息,格式为:"8/100"
    pageInfoLabel.text = [NSString stringWithFormat:@"%d/%d", currentPage+1, totalPages];
}


////////////////////////////////////////////////////////////////////////////////////////
// 上一页
- (IBAction)actionPrevious:(id)sender {
    if (currentPage > 0) {
        currentPage--;
        
        NSRange range = rangeOfPages[currentPage];
        NSString *pageText = [text substringWithRange:range];
        
        textLabel.text = pageText;

        //
        pageInfoLabel.text = [NSString stringWithFormat:@"%d/%d", currentPage+1, totalPages];
    }
}

////////////////////////////////////////////////////////////////////////////////////////
// 下一页
- (IBAction)actionNext:(id)sender {
    if (currentPage < totalPages-1) {
        currentPage++;
        
        NSRange range = rangeOfPages[currentPage];
        NSString *pageText = [text substringWithRange:range];
        
        textLabel.text = pageText;
        
        //
        pageInfoLabel.text = [NSString stringWithFormat:@"%d/%d", currentPage+1, totalPages];
    }
}

posted on 2010-09-13 10:01 seal 阅读(210) 评论(0)  编辑  收藏 所属分类: iPhone

只有注册用户登录后才能发表评论。


网站导航: