IPhone control-UITextField

Source: Internet
Author: User
  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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.