1 //
2 // UITestViewController.h
3 // UITest
4 //
5
6 #import <UIKit/UIKit.h>
7
8 @interface UITestViewController : UIViewController <UITextViewDelegate>
9 {
10
11 }
12
13 @end
14
15
16
17 //
18 // UITestViewController.m
19 // UITest
20 //
21
22 #import "UITestViewController.h"
23
24 UIScrollView *myScrollView;
25 UITextView *myTextView;
26
27 @implementation UITestViewController
28
29 -(void)buttonClick:(id)sender
30 {
31 [myTextView resignFirstResponder];
32 }
33
34 - (void)viewDidLoad {
35
36 [super viewDidLoad];
37
38 CGRect viewRect = [self.view bounds];
39 myScrollView = [[UIScrollView alloc] initWithFrame:viewRect];
40 myScrollView.contentSize = viewRect.size;
41 [self.view addSubview:myScrollView];
42
43 CGRect buttonFrame = CGRectMake(10,10,60,32);
44 UIButton *keyboardToggle = [UIButton buttonWithType:UIButtonTypeRoundedRect];
45 [keyboardToggle setTitle:@"hide" forState:UIControlStateNormal];
46 [keyboardToggle addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
47 keyboardToggle.frame = buttonFrame;
48 [myScrollView addSubview:keyboardToggle];
49
50 CGRect textRect = CGRectMake(10,60,300,200);
51 myTextView = [[UITextView alloc] initWithFrame:textRect];
52
53 myTextView.font = [UIFont systemFontOfSize:22.0];
54
55 myTextView.keyboardType = UIKeyboardTypeDefault;
56 myTextView.returnKeyType = UIReturnKeyGo;
57
58 //uncomment to enable data detectors
59 //myTextView.text = @"this is a link: http://google.com ";
60 //myTextView.dataDetectorTypes = UIDataDetectorTypeAll;
61 //myTextView.enabled = NO;
62
63 myTextView.delegate = self;
64
65 [myScrollView addSubview:myTextView];
66
67 [myTextView release];
68 [myScrollView release];
69 }
70
71 - (void)textViewDidBeginEditing:(UITextView *)textView
72 {
73 float keyboardHeight;
74 if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait | UIDeviceOrientationPortraitUpsideDown)
75 keyboardHeight = 216.0;
76 else
77 keyboardHeight = 162.0;
78
79 CGRect textViewRect = textView.frame;
80 float textViewBottom = textViewRect.origin.y + textViewRect.size.height;
81
82 CGRect viewRect = [myScrollView bounds];
83 float keyboardTop = viewRect.size.height-keyboardHeight;
84
85 float scrollOffset = fabs(textViewBottom - keyboardTop);
86
87 [myScrollView setContentOffset:CGPointMake(0, scrollOffset) animated:YES];
88 }
89
90 - (void)textViewDidEndEditing:(UITextView *)textView
91 {
92 [myScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
93 }
94 - (void)didReceiveMemoryWarning {
95 // Releases the view if it doesn't have a superview.
96 [super didReceiveMemoryWarning];
97
98 // Release any cached data, images, etc that aren't in use.
99 }
100
101 - (void)viewDidUnload {
102 // Release any retained subviews of the main view.
103 // e.g. self.myOutlet = nil;
104 }
105
106
107 - (void)dealloc {
108 [super dealloc];
109 }
110
111 @end
一种简单的按回车键后,键盘弹起的操作,实现UITextViewDelegate的下属方法:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text {
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}