#import "ViewController.h"
typedef enum whichView{
ChangeTextSize = 500,
ChangeTextColor,
isPagingEnabledView,
isDayView,
}whichView;
@interface ViewController ()<UIScrollViewDelegate,UITextViewDelegate>
{
NSString *content;
UITextView *showTextView;
UIView *bgview;
// 上一次点击按钮的tag
NSInteger lastSelectButtonTag;
// 是否显示工具栏
BOOL isHiddenToolBar;
// 颜色数组
NSArray *colorArray;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 颜色数组
colorArray = @[[UIColor colorWithRed:0.357 green:0.225 blue:0.312 alpha:1.000],[UIColor colorWithRed:0.800 green:0.400 blue:1.000 alpha:1.000],[UIColor colorWithRed:0.400 green:0.800 blue:1.000 alpha:1.000],[UIColor colorWithRed:0.400 green:1.000 blue:0.800 alpha:1.000],[UIColor colorWithRed:0.800 green:1.000 blue:0.400 alpha:1.000],[UIColor colorWithRed:1.000 green:0.100 blue:0.400 alpha:1.000],[UIColor colorWithRed:1.000 green:0.800 blue:0.400 alpha:1.000],[UIColor colorWithRed:0.400 green:0.300 blue:1.000 alpha:1.000],[UIColor colorWithRed:1.000 green:0.435 blue:0.812 alpha:1.000]];
// UITextView 是滚动视图的子类 文本视图 显示文本 输入文本
// 1.显示电子书(textview实现) 2.需要一个工具栏 工具栏上面可以设置字体大小(滑杆实现),字体颜色(view来实现,通过用户点击的view找到view上面的颜色,设置成字体的颜色),是否可以翻页(开关按钮。设置滚动视图是否翻页的属性),夜间模式(开关按钮,把背景颜色换成深色)
// 用户翻看小说的时候 隐藏工具栏 双击的时候显示工具栏
UIView *toolBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
toolBar.tag = 225;
toolBar.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:toolBar];
NSArray *title = @[@"字体大小",@"字体颜色",@"是否翻页",@"夜间模式"];
for (int i = 0; i < title.count; i ++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10+((([UIScreen mainScreen].bounds.size.width)-15-20)/4+5)*i, 20, (([UIScreen mainScreen].bounds.size.width)-15-20)/4, 44);
[button setTitle:title[i] forState:UIControlStateNormal];
button.tag = 100 + i;
button.titleLabel.font = [UIFont systemFontOfSize:17];
button.showsTouchWhenHighlighted = YES;
// button.selected = NO;
[button addTarget:self action:@selector(touchTo:) forControlEvents:UIControlEventTouchUpInside];
[toolBar addSubview:button];
}
showTextView = [[UITextView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(toolBar.frame), self.view.frame.size.width, self.view.frame.size.height-CGRectGetMaxY(toolBar.frame))];
[self showTextSize:[[NSUserDefaults standardUserDefaults] floatForKey:@"saveTextSize"] LabelContent:nil textViewFont:[[NSUserDefaults standardUserDefaults] floatForKey:@"saveFont"] ];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *contentOffset = [defaults objectForKey:@"阅读位置"];
// 判断一下 为了防止程序第一次运行 没有记录 用户阅读的位置 获取字典key的时候就会有可能崩溃
if (contentOffset != 0
) {
CGFloat x = [contentOffset[@"x"] floatValue];
CGFloat y = [contentOffset[@"y"] floatValue];
showTextView.contentOffset = CGPointMake(x,y);
}
showTextView.editable = NO;
showTextView.selectable = NO;
showTextView.delegate = self;
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSInteger colorTag = [user integerForKey:@"saveTextColor"];
// UIColor *textColor = [user objectForKey:@"saveTextColor"];
// 如果 用户设置了 字体颜色 显示用户设置的颜色
if (colorTag != 0 && colorTag < 1000) {
UIColor *textColor = colorArray[colorTag-900];
showTextView.textColor = textColor;
}
//
[self.view addSubview:showTextView];
// 双击显示工具栏
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(isNotHidden:)];
tap.numberOfTapsRequired = 2;
[showTextView addGestureRecognizer:tap];
[self loadData];
}
/**
* 加载Bundle文件
*/
- (void) loadData{
NSString *path = [[NSBundle mainBundle] pathForResource:@"无心法师.txt" ofType:nil];
NSError *error;
content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:& error];
// error后面如果写error,会把错误信息告诉用户
if (error) {
NSLog(@"%@",error);
}else{
// NSLog(@"%@",content);
showTextView.text = content;
}
}
/**
* 点击Tag值的按钮
*
* @param sender
*/
- (void) touchTo:(UIButton *)sender{
switch (sender.tag) {
case 100:
[self changeWithViewTag:ChangeTextSize andSelectButton:sender];
break;
case 101:
[self changeWithViewTag:ChangeTextColor andSelectButton:sender];
break;
case 102:
[self changeWithViewTag:isPagingEnabledView andSelectButton:sender];
break;
case 103:
[self changeWithViewTag:isDayView andSelectButton:sender];
break;
default:
break;
}
}
/**设置字体的大小*/
- (void) changeWithViewTag:(whichView)whichView andSelectButton:(UIButton *)sender{
// 重置上一个按钮的选中状态
if (lastSelectButtonTag !=0 && lastSelectButtonTag != sender.tag) {
UIButton *button = (UIButton *)[[self.view viewWithTag:225] viewWithTag:lastSelectButtonTag];
button.selected = NO;
}
sender.selected = !sender.selected;
UIView *view = [self.view viewWithTag:whichView];
// sender.selected == YES 用户选中的时候 (第一次点击的时候)
// 用户第一次点击的时候 初始化视图
if (sender.selected == YES) {
if (view == nil) {
[self showViewWithWhichView:whichView andButton:sender];
}
}else{
// 用户第二次点击的时候 移除视图 如果想完全让视图消失 必须让视图对象置空 设置成nil
// null 和nil 是不一样的
if (view != nil) {
[view removeFromSuperview];
view = nil;
}
}
lastSelectButtonTag = sender.tag;
}
#pragma mark ----- 创建弹出视图 -----
- (void) showViewWithWhichView:(whichView)whichView andButton:(UIButton *)sender{
lastSelectButtonTag = sender.tag;
for (int i = ChangeTextSize; i <= isDayView; i ++) {
UIView *view = [self.view viewWithTag:i];
[view removeFromSuperview];
view = nil;
}
bgview = [[UIView alloc]initWithFrame:CGRectMake(0, 100, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetWidth([UIScreen mainScreen].bounds))];
bgview.tag = whichView;
bgview.backgroundColor = [UIColor lightGrayColor];
bgview.alpha = 0.8;
[self.view addSubview:bgview];
UILabel *close = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth(bgview.frame)-30, 10, 20, 20)];
close.text = @"X";
close.textAlignment = NSTextAlignmentCenter;
close.textColor = [UIColor redColor];
close.backgroundColor = [UIColor whiteColor];
close.layer.cornerRadius = 20/2;
//不显示超出的部分 <两种方法>
close.clipsToBounds = YES;//不显示超出的部分
// close.layer.masksToBounds = YES;//不显示超出的部分
// [bgview addSubview:close];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapTo:)];
[showTextView addGestureRecognizer:tap];
switch (whichView) {
case ChangeTextSize:
{
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(10, 100, CGRectGetWidth(bgview.frame)-20, 40)];
slider.center = bgview.center;
[slider addTarget:self action:@selector(changeSize:) forControlEvents:UIControlEventValueChanged];
slider.minimumValue = 10;
slider.maximumValue = 50;
[bgview addSubview:slider];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// [defaults floatForKey:@"saveTextSize"];
UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, 80, 40)];
title.text = @"字体大小:";
[bgview addSubview:title];
UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth(title.frame), 20, 400, 40)];
contentLabel.tag = 10;
[self showTextSize:[defaults floatForKey:@"saveTextSize"] LabelContent:contentLabel textViewFont:[defaults floatForKey:@"saveFont"]];
[bgview addSubview:contentLabel];
}
break;
case ChangeTextColor:
{
NSInteger textColorTag = 0;
for (int i = 0; i < 3; i ++ ) {
for (int j = 0; j < 3; j ++) {
// 两个视图间的间距
CGFloat margin = 10;
// 颜色视图的宽度
CGFloat textColorW = (CGRectGetWidth(bgview.frame)-40-margin*2)/3;
UIView *textColor = [[UIView alloc]initWithFrame:CGRectMake(20+(margin+textColorW)*i, 20+(margin+textColorW)*j, textColorW, textColorW)];
textColor.tag = textColorTag +900;
textColor.backgroundColor = colorArray[textColorTag];
textColor.layer.cornerRadius = textColorW/2;
[bgview addSubview:textColor];
UITapGestureRecognizer *selectTextColor = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectTextColor:)];
[textColor addGestureRecognizer:selectTextColor];
textColorTag ++;
}
}
}
break;
case isPagingEnabledView:
{
// TODO:未完成的部分
}
break;
case isDayView:
{
showTextView.backgroundColor = [UIColor colorWithWhite:0.200 alpha:1.000];
// TODO:这里也是未完成的部分
}
break;
default:
break;
}
}
/**
* 改变字号
*
* @param sender 使用滑杆来改变
*/
- (void)changeSize:(UISlider *)sender{
// 保存用户设置的字号
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setFloat:sender.value forKey:@"saveTextSize"];//该单例存值
[defaults setFloat:sender.value forKey:@"saveFont"];
// [defaults setFloat:sender.value forKey:@"saveSliderValue"];
[defaults synchronize];// 保持同步
showTextView.font = [UIFont systemFontOfSize:sender.value];
UILabel *label = (UILabel *)[[self.view viewWithTag:ChangeTextSize] viewWithTag:10];
// 显示当前字号
[self showTextSize:sender.value LabelContent:label textViewFont:sender.value];
}
/**
* 根据字号 显示文字 和字体颜色的方法
*
* @param size
* @param label
* @param textView
*/
- (void) showTextSize:(float)size LabelContent:(UILabel *)label textViewFont:(float) textView{
if (size == 0) {
size = 10;
}
label.text = [NSString stringWithFormat:@" %0.1f号字",size];
if ( size >= 20) {
label.textColor = size >=30 ?[UIColor redColor]:[UIColor blackColor];
}else{
label.textColor = [UIColor greenColor];
}
showTextView.font = [UIFont systemFontOfSize:textView];
}
/**手势移除弹出视图*/
- (void) tapTo:(UITapGestureRecognizer *)sender{
UIButton *button = (UIButton *)[[self.view viewWithTag:225] viewWithTag:lastSelectButtonTag];
button.selected = NO;
[bgview removeFromSuperview];
bgview = nil;
}
/**
* 选择字体颜色
*
* @param sender
*/
- (void) selectTextColor:(UITapGestureRecognizer *)sender{
UIColor *selectColor = sender.view.backgroundColor;
showTextView.textColor = selectColor;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:sender.view.tag forKey:@"saveTextColor"];
[defaults synchronize];
}
/**记录用户观看小说的位置*/
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
// 把contentOffset坐标X Y保存到一个字典里面
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@{@"x":@(scrollView.contentOffset.x),@"y":@(scrollView.contentOffset.y)} forKey:@"阅读位置"];
[defaults synchronize];
NSLog(@"结束拖拽的时候 %f %f",showTextView.contentOffset.x,showTextView.contentOffset.y);
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@{@"x":@(scrollView.contentOffset.x),@"y":@(scrollView.contentOffset.y)} forKey:@"阅读位置"];
[defaults synchronize];
NSLog(@"结束降速的时候 %f %f",scrollView.contentOffset.x,scrollView.contentOffset.y);
}
/**用户开始拖拽 隐藏工具栏*/
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
if (isHiddenToolBar != YES) {
isHiddenToolBar = YES;
[UIView animateWithDuration:0.5 animations:^{
UIView *toolBar = [self.view viewWithTag:225];
toolBar.frame = CGRectMake(0, 0, self.view.frame.size.width, -64);
showTextView.frame = CGRectMake(0, CGRectGetMaxY(toolBar.frame), self.view.frame.size.width, self.view.frame.size.height-CGRectGetMaxY(toolBar.frame));
}];
}
}
/**显示工具栏*/
- (void) isNotHidden:(UITapGestureRecognizer *)sender{
if (isHiddenToolBar == YES) {
isHiddenToolBar = NO;
[UIView animateWithDuration:0.5 animations:^{
UIView *toolBar = [self.view viewWithTag:225];
toolBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 64);
showTextView.frame = CGRectMake(0, CGRectGetMaxY(toolBar.frame), self.view.frame.size.width, self.view.frame.size.height-CGRectGetMaxY(toolBar.frame));
}];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
奉上 无心法师txt文件一份
iOS UITextView的使用(小小阅读器实现)
最新推荐文章于 2017-08-29 23:57:49 发布