• 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
  • 相关阅读:
    IOS 学习 开发 自定义 UINavigationController 导航
    -canOpenURL: failed for URL: ""
    一种简单的直观的高效的权限设计
    查询最近24小时、昨天、最近7天、上周、最近30天、上月的数据记录
    MS SQLSERVER中如何快速获取表的记录总数
    MVC MVVM和传统三层的理解
    C# 后台模拟前台post发送json数据
    C# Post发送数据返回页面结果
    CSS同时使用背景图片和背景颜色
    C# 根据日期计算星期几
  • 原文地址:https://www.cnblogs.com/foxmin/p/2393644.html
Copyright © 2020-2023  润新知