Repository navigation
Expand file tree
/
Copy pathViewController.m
More file actions
102 lines (72 loc) · 3.14 KB
/
Copy pathViewController.m
File metadata and controls
102 lines (72 loc) · 3.14 KB
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
//
// ViewController.m
// CYRTextViewExample
//
// Created by Illya Busigin on 1/5/14.
// Copyright (c) 2014 Cyrillian, Inc. All rights reserved.
//
#import "ViewController.h"
#import "QEDTextView.h"
@interface ViewController () <UITextViewDelegate>
@property (nonatomic, strong) QEDTextView *textView;
@end
@implementation ViewController
#pragma mark - View Lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = @"CYRTextView";
self.view.backgroundColor = [UIColor whiteColor];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
QEDTextView *textView = [[QEDTextView alloc] initWithFrame:self.view.bounds];
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
textView.delegate = self;
self.textView = textView;
[self.view addSubview:textView];
self.textView.text = @"// Test comment\n\n"\
@"// Let's solve our first equation\n"\
@"x = 1 / (1 + x) // equation to solve for x\n"\
@"x > 0 // restrict to positive root\n\n"\
@"// Let's create a user function\n"\
@"// The standard function random returns a random value between 0 an 1\n"\
@"g(x) := 0.005 * (x + 1) * (x - 1) + 0.1 * (random - 0.5)\n\n"\
@"// Now let's plot the two functions together on one chart\n"\
@"plot f(x), g(x)";
}
#pragma mark - Notification Handlers
- (void)keyboardWillShow:(NSNotification*)aNotification
{
UIBarButtonItem *dismissButton = [[UIBarButtonItem alloc] initWithTitle:@"Dismiss" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyboard)];
[self.navigationItem setRightBarButtonItem:dismissButton animated:YES];
[self moveTextViewForKeyboard:aNotification up:YES];
}
- (void)keyboardWillHide:(NSNotification*)aNotification
{
self.navigationItem.rightBarButtonItem = nil;
[self moveTextViewForKeyboard:aNotification up:NO];
}
#pragma mark - Convenience
- (void)moveTextViewForKeyboard:(NSNotification*)aNotification up:(BOOL)up
{
NSDictionary* userInfo = [aNotification userInfo];
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
CGRect keyboardEndFrame;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
CGRect newFrame = _textView.frame;
CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];
newFrame.size.height -= keyboardFrame.size.height * (up?1:-1);
_textView.frame = newFrame;
[UIView commitAnimations];
}
- (void)dismissKeyboard
{
[_textView resignFirstResponder];
}
@end