-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextViewController.m
135 lines (112 loc) · 3.71 KB
/
TextViewController.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
//
// TextViewController.m
// QQApiDemo
//
// Created by Tencent on 12-5-16.
// Copyright 2012年 Tencent. All rights reserved.
//
#import "TextViewController.h"
@implementation TextViewController
@synthesize delegate = _delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"编辑文本消息";
self.view.backgroundColor = [UIColor whiteColor];
CGRect screenRect = [UIScreen mainScreen].bounds;
CGRect textViewRect = CGRectMake(10, 10, screenRect.size.width - 10 * 2, 150);
CGRect textLenLabelRect = CGRectMake(screenRect.size.width - 230, 130, 200, 30);
_textView = [[UITextView alloc]initWithFrame:textViewRect];
_textView.clipsToBounds = YES;
_textView.text = @"你见,或者不见我\
我就在那里\
不悲不喜\
你念,或者不念我\
情就在那里\
不来不去\
你爱,或者不爱我\
爱就在那里\
不增不减\
你跟,或者不跟我\
我的手就在你手里\
不舍不弃\
来我的怀里\
或者\
让我住进你的心里\
默然相爱\
寂静欢喜";
_textView.font = [UIFont systemFontOfSize:16];
_textView.textAlignment = UITextAlignmentLeft;
_textView.contentOffset = CGPointMake(0, 10);
_textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
_textView.autocorrectionType = UITextAutocorrectionTypeYes;
_textView.scrollEnabled = YES;
_textView.scrollsToTop = YES;
_textView.showsHorizontalScrollIndicator = YES;
_textView.enablesReturnKeyAutomatically = YES;
_textView.returnKeyType = UIReturnKeyDone;
_textView.delegate = self;
[self.view addSubview:_textView];
[_textView becomeFirstResponder];
_textLenLabel = [[UILabel alloc] initWithFrame:textLenLabelRect];
_textLenLabel.text = @"0";
_textLenLabel.textAlignment = UITextAlignmentRight;
_textLenLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:_textLenLabel];
_textLenLabel.text = [NSString stringWithFormat:@"%d", [_textView.text length]];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(doneAction:)] autorelease];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(backAction:)] autorelease];
}
- (void)viewDidUnload
{
[super viewDidUnload];
[_textView release];
[_textLenLabel release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)textViewDidChange:(UITextView *)textView
{
int len = [textView.text length];
_textLenLabel.text = [NSString stringWithFormat:@"%d", len];
}
- (void)doneAction:(id)sender
{
if(_delegate)
{
[_delegate onTextViewDone:_textView.text];
}
}
- (void)backAction:(id)sender
{
if(_delegate)
{
[_delegate onTextViewCancel];
}
}
@end