思路: 1.new 一個 Empty 尾碼為 .txt 文件,內容隨筆拷貝一段 2.用 NSString 接收本地文件,再用一個標題拼接字元串 3.創建一個 NSMutableParagraphStyle 實例,設置標題居中、標題距離正文間隔 4.創建一個 NSMutableParagraphSty ...
思路:
1.new 一個 Empty 尾碼為 .txt 文件,內容隨筆拷貝一段
2.用 NSString 接收本地文件,再用一個標題拼接字元串
3.創建一個 NSMutableParagraphStyle 實例,設置標題居中、標題距離正文間隔
4.創建一個 NSMutableParagraphStyle 實例,設置正文左對齊、每段開頭縮進 10
5.創建一個基礎動畫的實例,用代理監聽動畫結束
話不多說上代碼
#pragma mark -- 拼接字元
- (void)loadData:(NSString *)filename {
//第一個段落
NSMutableParagraphStyle *first = [[NSMutableParagraphStyle alloc]init];
first.alignment = NSTextAlignmentCenter;
first.lineSpacing = 10;
//第二個段落
NSMutableParagraphStyle *second = [[NSMutableParagraphStyle alloc]init];
//從左開始寫
second.alignment = NSTextAlignmentLeft;
//首行縮進
second.firstLineHeadIndent = 10;
//間距
second.lineSpacing = 10;
UIFont *titleFont = [UIFont systemFontOfSize:16];
UIFont *contextFont = [UIFont systemFontOfSize:13];
NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc]initWithString:@"游戲許可及服務協議" attributes:@{NSParagraphStyleAttributeName:first,NSFontAttributeName:titleFont}];
//標題拼接正文,正文前加換行符
NSString *str = [NSString stringWithFormat:@"\n%@",filename];
NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc]initWithString:str attributes:@{NSParagraphStyleAttributeName:second,NSFontAttributeName:contextFont}];
[str1 appendAttributedString:str2];
_textView.attributedText = str1;
}
#pragma mark -- 關閉動畫
- (void)closeAniamtion:(UIView *)view {
CABasicAnimation *animation = [CABasicAnimation animation];
animation.keyPath = @"transform.scale";
animation.toValue = @0.9;
animation.duration = 0.4;
animation.delegate = self;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[view.layer addAnimation:animation forKey:@"closeAnimation"];
}
#pragma mark -- 本地 txt 文件內容
- (NSString *)filename {
//本地 txt 文件
NSString *path = [[NSBundle mainBundle]pathForResource:@"淺遇時光,靜好無恙.txt" ofType:nil];
//取出內容
NSString *filename = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
return filename;
}
#pragma mark -- 代理監聽動畫停止
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
if ([_bgView.layer animationForKey:@"closeAnimation"] == anim) {
[_bgView removeFromSuperview];
}
}
完整代碼在 github 上,點我下載