-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathXEditViewController.m
107 lines (85 loc) · 4.02 KB
/
XEditViewController.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
#import "XEditViewController.h"
@interface XEditViewController ()
@property (nonatomic, strong) MMTextView *textView;
@end
@implementation XEditViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initNav];
[self initSubviews];
[self setup];
}
- (void)initNav {
self.navigationItem.leftBarButtonItem = [objc_getClass("MMUICommonUtil") getBarButtonWithTitle:@"返回" target:self action:@selector(onBack) style:3];
self.navigationItem.rightBarButtonItem = [objc_getClass("MMUICommonUtil") getBarButtonWithTitle:@"完成" target:self action:@selector(onFinfsh) style:4];
[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:18.0]}];
}
- (void)initSubviews {
self.textView = ({
MMTextView *tv = [[objc_getClass("MMTextView") alloc] initWithFrame:CGRectMake(7, 0, SCREEN_WIDTH - 14, SCREEN_HEIGHT)];
tv.font = [UIFont systemFontOfSize:16];
tv;
});
[self.view addSubview:self.textView];
}
- (void)setup {
self.textView.text = self.text;
self.textView.placeholder = self.placeholder;
self.view.backgroundColor = [UIColor whiteColor];
[self.textView becomeFirstResponder];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
- (void)keyBoardChange:(NSNotification *)note {
CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
if (keyboardFrame.origin.y < SCREEN_HEIGHT) {
self.textView.frame = CGRectMake(7, 0, SCREEN_WIDTH - 14, SCREEN_HEIGHT - keyboardFrame.size.height);
} else {
self.textView.frame = CGRectMake(7, 0, SCREEN_WIDTH - 14, SCREEN_HEIGHT);
}
}
- (void)onBack {
if (self.textView.text.length > 0 && ![self.textView.text isEqualToString:self.text]) {
[self alertControllerWithTitle:@"确定不保存返回么"
message:nil
leftBlock:nil
rightBlock:^() {
[self.view endEditing:YES];
[self.navigationController popViewControllerAnimated:YES];
}];
} else {
[self.view endEditing:YES];
[self.navigationController popViewControllerAnimated:YES];
}
}
- (void)onFinfsh {
[self.view endEditing:YES];
if (self.endEditing) {
self.endEditing(self.textView.text);
}
[self.navigationController popViewControllerAnimated:YES];
}
- (void)alertControllerWithTitle:(NSString *)title message:(NSString *)message leftBlock:(void (^)(void))leftBlk rightBlock:(void (^)(void))rightBlk {
UIAlertController *alertController = ({
UIAlertController *alert = [UIAlertController
alertControllerWithTitle:title
message:nil
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"取消"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * _Nonnull action) {
if (leftBlk) {
leftBlk();
}
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"确定"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
if (rightBlk) {
rightBlk();
}
}]];
alert;
});
[self presentViewController:alertController animated:YES completion:nil];
}
@end