天天看點

IOS------電子書基本建立

一、通過建立EBookView類,繼承于UIView,加載電子書

1.給電子書添加幾個屬性:

//接收外部傳遞過來的資料
@property (nonatomic, copy) NSString *text;
//設定字型
@property (nonatomic, strong) UIFont *font;
//是否需要動畫
@property (nonatomic, assign) BOOL animated;
           

2.在.m檔案裡面完成對電子書的顯示和操作

(1)建立UILabel顯示電子書

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self != nil){
        //建立label用于顯示文本資訊
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        //設定背景顔色
        _label.backgroundColor = [UIColor whiteColor];
        //取消label的單行顯示
        _label.numberOfLines = 0;
        //設定電子書的字型為本身字型
        _label.font = self.font;
        //将電子書顯示
        [self addSubview:_label];   
        //不能在這裡完成(擷取第一頁的内容 顯示出來)因為initWithFrame的時候text還沒有資料
        //初始化數組
        self.pageRangesArray = [NSMutableArray array]; 
        self.animated = YES;
    }
    return self;
}
           

2.擷取第一頁内容,重寫set方法

- (void)setText:(NSString *)aText{
    _text = [aText copy];
    //顯示第一頁的内容
    self.label.font = self.font;
    //計算頁數
    NSRange firstPageRange = [self calculatePageRange:0];
    self.label.text = [self.text substringWithRange:firstPageRange];
}
           

3.重寫font的get方法

//重寫font的get方法
- (UIFont *)font{
    if(_font == nil){
        //外部沒有設定  給一個預設值
        self.font = [UIFont systemFontOfSize:16];
    }
    return _font;
}
           

4.顯示電子書章節内容

//計算之前得確定是有資料的
- (NSRange)calculatePageRange:(NSInteger)page{
    NSRange range = NSMakeRange(0, 0);
    //将上一頁的range拿出來
    if (page > 0){
        NSValue *rgValue = [self.pageRangesArray objectAtIndex:page-1];
        NSRange preRange = [rgValue rangeValue];  
        //記錄page也對應的起始位置
        range.location = preRange.location + preRange.length;
    } 
    for (int i = 0; i < self.text.length; i++) {
        //改變length的值
        range.length++;     
        //擷取目前這個range對應子字元串
        NSString *subString = [self.text substringWithRange:range];     
        //計算subString的高度
        CGSize realSize = [subString boundingRectWithSize:CGSizeMake(self.label.bounds.size.width, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.font} context:nil].size;   
        //判斷是否超過這個顯示的高度了
        if (realSize.height > self.label.bounds.size.height){
            //讓range儲存這一頁的長度
            range.length --;
            //儲存這個range
            NSValue *rgValue = [NSValue valueWithRange:range];
            [self.pageRangesArray addObject:rgValue];
             return range;
        }
    }
    //最後一頁
    if(range.length > 0){
        //儲存這個range
        NSValue *rgValue = [NSValue valueWithRange:range];
        [self.pageRangesArray addObject:rgValue];     
        return range;
    }else{
        return NSMakeRange(0, 0);
    }
}
           

5.點選事件開始

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //擷取UITouch對象
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self]; 
    NSRange currentPageRange = [[self.pageRangesArray objectAtIndex:_currentPage] rangeValue];
    //判斷上一頁還是下一頁
    if (location.x > self.center.x){
        //下一頁
        //判斷是否越界了
        if (currentPageRange.location + currentPageRange.length < self.text.length){
            [self changePage:_currentPage+1];
        }
    } else{
        //上一頁
        if (_currentPage > 0){
            [self changePage:_currentPage-1];
        }
    }
}
           

6.翻書

//切換頁面 如果有了就不用加載  如果沒有就需要加載
- (void)changePage:(NSInteger)page{
    //判斷數組裡面有沒有
    NSRange range;
    if (page > self.pageRangesArray.count-1){
        //需要計算下一頁的内容
        range = [self calculatePageRange:page];
        if (self.animated){
            [self animateWithType:kAnimationTypeUp];
        }
    } else{
        //數組裡面已經計算過了 不需要計算 直接拿出來使用
        range = [[self.pageRangesArray objectAtIndex:page] rangeValue];
        if (self.animated){
            [self animateWithType:kAnimationTypeDown];
        }
    }
    self.currentPage = page;
    self.label.text = [self.text substringWithRange:range];
}
           

7.翻書的效果動畫

- (void)animateWithType:(kAnimationType)type{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    if (type == kAnimationTypeUp){
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self cache:NO];
    } else{
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self cache:NO];
    }
    [UIView commitAnimations];
}
           

二、在主界面設定電子書

1.懶加載電子書,需要時再建立

#pragma mark ------- initUI ---------
- (void)initUI{
    //設定标題
    self.title = @"電子書";   
    //設定背景顔色
    self.view.backgroundColor = [UIColor whiteColor];    
    //建立一個電子書
    EBookView *bookView = [[EBookView alloc] initWithFrame:CGRectMake(0, 20+44, self.view.bounds.size.width, self.view.bounds.size.height - 20-44)];
    bookView.text = [self loadData];
    bookView.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:bookView];
}
           

2.加載電子書,擷取路徑

- (NSString *)loadData{
    //從檔案讀取資料
    //擷取路徑
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"登山拜師.txt" ofType:nil];  
    //讀取檔案的内容
    return [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
}