iOS學習,ios學習路線
思路:
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 上,點我下載