-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTaskNoteViewController.m
147 lines (121 loc) · 4.98 KB
/
TaskNoteViewController.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
141
142
143
144
145
146
147
//
// TaskNoteViewController.m
// gTask
//
// Created by LIANGJUN JIANG on 3/31/13.
//
//
#import "TaskNoteViewController.h"
@interface TaskNoteViewController ()<UITextViewDelegate>
@property (nonatomic, strong) UITextView *textView;
@end
@implementation TaskNoteViewController
@synthesize textView, task;
-(id)initWithTask:(GTLTasksTask *)mTask
{
self = [super initWithNibName:@"TaskNoteViewController" bundle:nil];
if (self) {
self.task = mTask;
}
return self;
}
- (void)setupTextView
{
self.textView = [[UITextView alloc] initWithFrame:self.view.frame];
self.textView.textColor = [UIColor blackColor];
self.textView.font = [UIFont fontWithName:@"Arial" size:18.0];
self.textView.delegate = self;
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.text = self.task.notes;
self.textView.returnKeyType = UIReturnKeyDefault;
self.textView.keyboardType = UIKeyboardTypeDefault; // use the default type input method (entire keyboard)
self.textView.scrollEnabled = YES;
// this will cause automatic vertical resize when the table is resized
self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
// note: for UITextView, if you don't like autocompletion while typing use:
// myTextView.autocorrectionType = UITextAutocorrectionTypeNo;
[self.view addSubview: self.textView];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = NSLocalizedString(@"Note", @"");
[self setupTextView];
// DebugLog(@"this task has notes: %@",self.task);
}
// called after the view controller's view is released and set to nil.
// For example, a memory warning which causes the view to be purged. Not invoked as a result of -dealloc.
// So release any properties that are loaded in viewDidLoad or can be recreated lazily.
//
- (void)viewDidUnload
{
[super viewDidUnload];
self.textView = nil;
}
- (void)keyboardWillShow:(NSNotification *)aNotification
{
// the keyboard is showing so resize the table's height
CGRect keyboardRect = [[aNotification userInfo][UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval animationDuration =
[[aNotification userInfo][UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame;
frame.size.height -= keyboardRect.size.height;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
// the keyboard is hiding reset the table's height
CGRect keyboardRect = [[aNotification userInfo][UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval animationDuration =
[[aNotification userInfo][UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame;
frame.size.height += keyboardRect.size.height;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// listen for keyboard hide/show notifications so we can properly adjust the table's height
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
#pragma mark -
#pragma mark UITextViewDelegate
- (void)saveAction:(id)sender
{
// finish typing text/dismiss the keyboard by removing it as the first responder
//
[self.textView resignFirstResponder];
self.navigationItem.rightBarButtonItem = nil; // this will remove the "save" button
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
// provide my own Save button to dismiss the keyboard
UIBarButtonItem* saveItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(saveAction:)];
self.navigationItem.rightBarButtonItem = saveItem;
}
@end