UITextView(2)
2015-10-15 23:34 真实16 阅读(145) 评论(0) 编辑 收藏 举报//
// ViewController.m
// UI-NO-16-1
//
// Created by Bruce on 15/8/8.
// Copyright (c) 2015年 Bruce. All rights reserved.
//
#import "ViewController.h"
typedef enum WitchView{
ChangeTextSizeView = 500,
ChangeTextColorView,
IspagingEnabledView,
IsDayView,
}WitchView;
@interface ViewController ()<UITextViewDelegate>
{
NSString *content;
UITextView *showTextView;
// 上一次点击按钮的tag
NSInteger lastSelectButtonTag;
// 是否隐藏了工具栏
BOOL isHidenToolBar;
// 颜色数组
NSArray *colorArray;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1、显示电子书 (UITextView实现)
// 2、需要一个工具栏 工具栏上面 可以设置 字体大小 、 字体颜色、是否翻页、 夜间模式
// 3、字体大小 滑杆实现
// 4、字体颜色 uiview 实现 (通过 用户点击的view 找到view上面的颜色 设置成字体颜色)
// 5、是否翻页 开关 (设置 滚动视图 是否翻页的属性)
// 6、夜间模式 开关 (把背景颜色 换成黑色)
// 用户翻看小说的时候 隐藏工具栏 双击的时候 显示工具栏
colorArray = @[[UIColor colorWithRed:1.000 green:0.435 blue:0.812 alpha:1.000],[UIColor colorWithRed:0.800 green:0.400 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.800 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.800 blue:1.000 alpha:1.000],[UIColor colorWithRed:1.000 green:0.435 blue:0.812 alpha:1.000],[UIColor redColor]];
UIView *toolBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 64)];
toolBar.tag = 119;
toolBar.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:toolBar];
NSArray *titles = @[@"字体大小",@"字体颜色",@"是否翻页",@"夜间模式"];
for (int i=0; i<4; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10+(80+5)*i, 20, 80, 44);
[button setTitle:titles[i] forState:UIControlStateNormal];
button.tag = 100+i;
button.showsTouchWhenHighlighted = YES;
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
[toolBar addSubview:button];
}
showTextView = [[UITextView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(toolBar.frame), CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds)-CGRectGetMaxY(toolBar.frame))];
showTextView.delegate = self;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSInteger colorTag = [userDefaults integerForKey:@"saveTextColor"];
// 如果 用户设置了 字体颜色 显示用户设置的颜色
if (colorTag != 0 && colorTag < 1000) {
UIColor *textColor = colorArray[colorTag-900];
showTextView.textColor = textColor;
}
[self showTextSize:[[NSUserDefaults standardUserDefaults] floatForKey:@"saveTextSize"] andLabelContent:nil];
// 设置UITextView是否允许编辑
showTextView.editable = NO;
// 设置UITextView是否允许选择
showTextView.selectable = NO;
[self.view addSubview:showTextView];
// 双击显示工具栏
UITapGestureRecognizer *hidenToolBarGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showToolBar)];
hidenToolBarGesture.numberOfTapsRequired = 2;
[showTextView addGestureRecognizer:hidenToolBarGesture];
[self loadData];
}
- (void)loadData
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"无心法师" ofType:@"txt"];
NSError *error;
content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (error) {
NSLog(@"%@",error);
}else{
// NSLog(@"%@",content);
showTextView.text = content;
}
}
- (void)viewWillAppear:(BOOL)animated
{
// 获得 用户阅读的位置
NSDictionary *contSet = [[NSUserDefaults standardUserDefaults] objectForKey:@"阅读位置"];
// 为了防止程序第一次运行 没有记录 用户阅读的位置
// 获取字典里面的key的时候 就有可能崩溃(字典里不存在@“x”@"y"这两个key)
if (contSet.count!=0) {
CGFloat x = [contSet[@"x"] floatValue];
CGFloat y = [contSet[@"y"] floatValue];
showTextView.contentOffset = CGPointMake(x, y);
NSLog(@"%f",showTextView.contentOffset.y);
}
}
- (void)action:(UIButton *)sender
{
switch (sender.tag) {
case 100:
[self changeWithViewTag:ChangeTextSizeView andSelectedButton:sender];
break;
case 101:
[self changeWithViewTag:ChangeTextColorView andSelectedButton:sender];
break;
case 102:
[self changeWithViewTag:IspagingEnabledView andSelectedButton:sender];
break;
case 103:
[self changeWithViewTag:IsDayView andSelectedButton:sender];
break;
default:
break;
}
}
//设置
- (void)changeWithViewTag:(WitchView)witchView andSelectedButton:(UIButton *)sender
{
NSLog(@"%d",sender.selected);
// 如果 有点击过按钮 并且 点击的 不是同一个按钮 就重置 上一个按钮的选中状态
if (lastSelectButtonTag!=0 && lastSelectButtonTag != sender.tag) {
// 重置 按钮的 状态
UIButton *button = (UIButton *)[[self.view viewWithTag:119] viewWithTag:lastSelectButtonTag];
button.selected = NO;
}
sender.selected = !sender.selected;
// sender.selected==YES 用户选中的时候 (用户第一次点击的时候)
UIView *view = [self.view viewWithTag:witchView];
// 用户选中的时候 初始化视图
if (sender.selected==YES) {
if (!view) {
[self showViewWithWitchView:witchView andButton:sender];
}
}else{
// 用户未选中的时候 移除视图 (如果想完全让视图 消失 必须让视图对象=nil)
if (![view isEqual:[NSNull null]]) {
[view removeFromSuperview];
view = nil;
}
}
lastSelectButtonTag = sender.tag;
}
#pragma mark ------------- 创建 弹出视图
- (void)showViewWithWitchView:(WitchView)witchView andButton:(UIButton *)sender
{
for (int i= ChangeTextSizeView; i<=IsDayView; i++) {
UIView *view = [self.view viewWithTag:i];
[view removeFromSuperview];
view = nil;
}
UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 100, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetWidth([UIScreen mainScreen].bounds))];
bgView.tag = witchView;
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.layer.masksToBounds = YES;
[bgView addSubview:close];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeView:)];
[bgView addGestureRecognizer:tap];
switch (witchView) {
case ChangeTextSizeView:
{
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, 80, 40)];
titleLabel.text = @"字体大小:";
// titleLabel.backgroundColor = [UIColor redColor];
[bgView addSubview:titleLabel];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(titleLabel.frame), 20, 200, 40)];
contentLabel.tag = 10;
// contentLabel.backgroundColor = [UIColor yellowColor];
[bgView addSubview:contentLabel];
[self showTextSize:[userDefaults floatForKey:@"saveTextSize"] andLabelContent:contentLabel];
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(10, 100, CGRectGetWidth(bgView.frame)-20, 20)];
slider.center = bgView.center;
[slider addTarget:self action:@selector(changeSize:) forControlEvents:UIControlEventValueChanged];
slider.minimumValue = 10;
slider.maximumValue = 50;
slider.value = [userDefaults floatForKey:@"saveTextSize"];
[bgView addSubview:slider];
}
break;
case ChangeTextColorView:
{
// 两个颜色视图之间的间距
CGFloat space = 10;
// 每一个颜色视图的宽度
CGFloat Width = (CGRectGetWidth(bgView.frame)-40-space*2)/3;
NSInteger index = 0;
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(20+(Width+space)*j, 20+(Width+space)*i, Width, Width)];
view.tag = index+900;
view.backgroundColor = colorArray[index];
view.layer.cornerRadius = Width/2;
[bgView addSubview:view];
// 通过手势 选择 视图颜色
UITapGestureRecognizer *chooseColorGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chooseColor:)];
[view addGestureRecognizer:chooseColorGesture];
index ++;
}
}
}
break;
case IspagingEnabledView:
{
}
break;
case IsDayView:
{
}
break;
default:
break;
}
}
#pragma mark --------选择字体颜色
- (void)chooseColor:(UITapGestureRecognizer *)sender
{
// 先找到 手势上的视图 正在找到 视图的背景颜色
UIColor *chooseTextColor = sender.view.backgroundColor;
showTextView.textColor = chooseTextColor;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:sender.view.tag forKey:@"saveTextColor"];
[userDefaults synchronize];
}
#pragma mark --------- 手势 移除弹出视图
- (void)removeView:(UITapGestureRecognizer *)sender
{
UIButton *button = (UIButton *)[[self.view viewWithTag:119] viewWithTag:lastSelectButtonTag];
button.selected = NO;
UIView *view = sender.view;
[view removeFromSuperview];
view = nil;
}
- (void)changeSize:(UISlider *)sender
{
// 保存 用户 设置的 字号
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setFloat:sender.value forKey:@"saveTextSize"];
[userDefaults synchronize];
// 显示 当前的字号
UILabel *label = (UILabel *)[[self.view viewWithTag:ChangeTextSizeView] viewWithTag:10];
[self showTextSize:sender.value andLabelContent:label];
}
// 根据 字号 显示文字 和 字体颜色的方法
- (void)showTextSize:(float)size andLabelContent:(UILabel *)label
{
// 如果 用户没有设置 字号 就用系统默认的字号 10
if (size==0) {
size = 10;
}
label.text = [NSString stringWithFormat:@"%0.1f号字",size];
// 改变 textView的字号
showTextView.font = [UIFont systemFontOfSize:size];
if (size >= 20) {
label.textColor = size>=30 ? [UIColor redColor]:[UIColor blackColor];
}else{
label.textColor = [UIColor greenColor];
}
}
#pragma mark -----滚动代理方法
// 记录用户观看小说的位置
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
// NSLog(@"结束拖拽: x:%f y:%f",showTextView.contentOffset.x,showTextView.contentOffset.y);
NSLog(@"结束拖拽: x:%f y:%f",scrollView.contentOffset.x,scrollView.contentOffset.y);
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:@{@"x":@(scrollView.contentOffset.x),@"y":@(scrollView.contentOffset.y)} forKey:@"阅读位置"];
[userDefaults synchronize];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"结束降速: x:%f y:%f",scrollView.contentOffset.x,scrollView.contentOffset.y);
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:@{@"x":@(scrollView.contentOffset.x),@"y":@(scrollView.contentOffset.y)} forKey:@"阅读位置"];
[userDefaults synchronize];
}
//用户开始拖拽 隐藏工具栏
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
if (isHidenToolBar != YES) {
// 通过isHidenToolBar 来记录 是否已经隐藏了工具栏
isHidenToolBar = YES;
[UIView animateWithDuration:0.3 animations:^{
UIView *toolBar = [self.view viewWithTag:119];
toolBar.frame = CGRectMake(0, -64, CGRectGetWidth([UIScreen mainScreen].bounds), 64);
showTextView.frame = CGRectMake(0, CGRectGetMaxY(toolBar.frame), CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds)-CGRectGetMaxY(toolBar.frame));
}];
}
}
// 显示工具栏
- (void)showToolBar
{
if (isHidenToolBar==YES) {
isHidenToolBar = NO;
[UIView animateWithDuration:0.3 animations:^{
UIView *toolBar = [self.view viewWithTag:119];
toolBar.frame = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 64);
showTextView.frame = CGRectMake(0, CGRectGetMaxY(toolBar.frame), CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds)-CGRectGetMaxY(toolBar.frame));
}];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end