iPhone控件之UITextField

  1 //
2 // UITestViewController.h
3 // UITest
4 //
5
6 #import <UIKit/UIKit.h>
7
8 @interface UITestViewController : UIViewController <UITextFieldDelegate>
9 {
10
11 }
12
13 @end
14
15
16
17
18 //
19 // UITestViewController.m
20 // UITest
21 //
22
23 #import "UITestViewController.h"
24
25 @implementation UITestViewController
26
27 - (void)viewDidLoad {
28
29 [super viewDidLoad];
30
31 CGRect textRect = CGRectMake(10,10,300,31);
32 UITextField *myTextField = [[UITextField alloc] initWithFrame:textRect];
33 myTextField.borderStyle = UITextBorderStyleRoundedRect;
34
35 myTextField.font = [UIFont systemFontOfSize:22.0];
36 myTextField.adjustsFontSizeToFitWidth = YES;
37 myTextField.minimumFontSize = 2.0;
38
39 myTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
40 myTextField.keyboardType = UIKeyboardTypeDefault;
41
42 myTextField.autocorrectionType = UITextAutocorrectionTypeNo;
43 myTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
44 myTextField.returnKeyType = UIReturnKeyDone;
45
46 //myTextField.secureTextEntry = YES;
47
48 myTextField.delegate = self;
49
50 [self.view addSubview:myTextField];
51
52 //force keyboard to show immediately
53 [myTextField becomeFirstResponder];
54
55 [myTextField release];
56 }
57
58 - (BOOL)textFieldShouldReturn:(UITextField *)textField
59 {
60 [textField resignFirstResponder];
61 return YES;
62 }
63
64 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
65 {
66 /*
67 //limit text field to numeric values only
68 NSCharacterSet *numberSet = [NSCharacterSet decimalDigitCharacterSet];
69 for (NSUInteger i=0; i<[string length]; i++)
70 {
71 unichar ch = [string characterAtIndex:i];
72 if (![numberSet characterIsMember:ch])
73 return NO;
74 }
75 return YES;
76 */
77
78 //limit text field to 10 chars
79 int MAX_CHARS = 10;
80 NSMutableString *newText = [NSMutableString stringWithString:textField.text];
81 [newText replaceCharactersInRange:range withString:string];
82 return ([newText length] <= MAX_CHARS);
83 }
84
85 - (void)didReceiveMemoryWarning {
86 // Releases the view if it doesn't have a superview.
87 [super didReceiveMemoryWarning];
88
89 // Release any cached data, images, etc that aren't in use.
90 }
91
92 - (void)viewDidUnload {
93 // Release any retained subviews of the main view.
94 // e.g. self.myOutlet = nil;
95 }
96
97
98 - (void)dealloc {
99 [super dealloc];
100 }
101
102 @end
posted @ 2012-03-13 13:16  FoxBabe  阅读(306)  评论(0编辑  收藏  举报