forked from soffes/cheddar-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDIRenameTaskViewController.m
140 lines (101 loc) · 4.36 KB
/
CDIRenameTaskViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// CDIRenameTaskViewController.m
// Cheddar for iOS
//
// Created by Sam Soffes on 5/30/12.
// Copyright (c) 2012 Nothing Magical. All rights reserved.
//
#import "CDIRenameTaskViewController.h"
#import "UIColor+CheddariOSAdditions.h"
#import "UIFont+CheddariOSAdditions.h"
@interface CDIRenameTaskViewController () <UITextViewDelegate>
- (void)_keyboardDidShow:(NSNotification *)notification;
- (void)_keyboardDidHide:(NSNotification *)notification;
- (void)_updateTextViewFrame;
@end
@implementation CDIRenameTaskViewController {
CGRect _keyboardRect;
}
@synthesize task = _task;
@synthesize textView = _textView;
#pragma mark - NSObject
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Edit Task";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(save:)];
self.view.backgroundColor = [UIColor whiteColor];
_textView = [[SSTextView alloc] initWithFrame:self.view.bounds];
_textView.delegate = self;
_textView.autocapitalizationType = UITextAutocapitalizationTypeSentences;
_textView.autocorrectionType = UITextAutocorrectionTypeYes;
_textView.textColor = [UIColor cheddarTextColor];
_textView.placeholderTextColor = [UIColor cheddarLightTextColor];
_textView.placeholder = @"What do you have to do?";
_textView.returnKeyType = UIReturnKeyGo;
_textView.font = [UIFont cheddarFontOfSize:18.0f];
_textView.text = self.task.text;
[self.view addSubview:_textView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[_textView becomeFirstResponder];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
[self _updateTextViewFrame];
} completion:nil];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return YES;
}
return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
#pragma mark - Actions
- (void)save:(id)sender {
self.task.text = self.textView.text;
[self.task save];
[self.task update];
[self.navigationController dismissModalViewControllerAnimated:YES];
}
- (void)cancel:(id)sender {
[self.navigationController dismissModalViewControllerAnimated:YES];
}
#pragma mark - Private
- (void)_updateTextViewFrame {
CGSize size = self.view.bounds.size;
CGFloat heightAdjust = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? 0.0f : fminf(_keyboardRect.size.width, _keyboardRect.size.height);
_textView.frame = CGRectMake(0.0f, 0.0f, size.width, size.height - heightAdjust);
}
- (void)_keyboardDidShow:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
_keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
[self _updateTextViewFrame];
} completion:nil];
}
- (void)_keyboardDidHide:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
_keyboardRect = CGRectZero;
CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
[self _updateTextViewFrame];
} completion:nil];
}
#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqual:@"\n"]) {
[self save:textView];
return NO;
}
return YES;
}
@end