iOS UITextFiled 控件 限制其输入长度(手机号限长)

MyViewController.h

#import <UIKit/UIKit.h>

@interface MyViewController : UIViewController<UITextFieldDelegate>

@property (retain, nonatomic) NSMutableString *pStr;
@end

MyViewController.m

#import "MyViewController.h"

@interface MyViewController ()

@end

@implementation MyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
//	self.pStr =[[NSMutableString alloc]init];
    
    UITextField *text=[[UITextField alloc]initWithFrame:CGRectMake(60, 100, 200, 35)];
    //style
    text.borderStyle=UITextBorderStyleRoundedRect;
    text.tag=1;
    
//    text.text=@"999";
    //文本颜色
    text.textColor=[UIColor redColor];
    //文本的Alignment
    text.textAlignment=NSTextAlignmentLeft;
    //提示信息
    text.placeholder=@"提示";
    //适应大小
    text.adjustsFontSizeToFitWidth=YES;
    //代理
    text.delegate=self;
    //清除
    text.clearsOnBeginEditing=YES;
    //clearButtonMode
    text.clearButtonMode=UITextFieldViewModeUnlessEditing;
    //称为第一响应者
    [text becomeFirstResponder];
    
    //添加 右视图
//    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
//    view.backgroundColor=[UIColor yellowColor];
//    text.rightView=view;
//    [view release];
    
    //rightViewMode
    text.rightViewMode=UITextFieldViewModeUnlessEditing;
    //添加textFiled
    [self.view addSubview:text];
    [text release];
    
    //自定义键盘
    
//    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
//    view.backgroundColor=[UIColor yellowColor];
//    text.inputAccessoryView=view;
//    [view release];
    
//    text.secureTextEntry=YES;
//    text.keyboardType=UIKeyboardTypeNumberPad;
    text.returnKeyType=UIReturnKeySend;
    text.autocapitalizationType=UITextAutocapitalizationTypeNone;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
   
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITextField *tf=(UITextField *)[self.view viewWithTag:1];
    [tf resignFirstResponder];
}

#pragma mark---TextDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField// return NO to disallow editing.
{
//    NSLog(@"textFieldShouldBeginEditing");
    return YES;
    
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//    NSLog(@"textFieldDidBeginEditing");
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField         // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
{
//    NSLog(@"textFieldShouldEndEditing");
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
//    NSLog(@"textFieldDidEndEditing");
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  // return NO to not change text
{
    UITextField *text=(UITextField *)[self.view viewWithTag:1];
    
    //限制其输入长度,电话号码输入限长
    self.pStr=[[NSMutableString alloc]initWithFormat:@"%@",text.text ];
    if (self.pStr.length >=11) {
        return NO;
    }
    
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField              // called when clear button pressed. return NO to ignore (no notifications)
{
    NSLog(@"textFieldShouldClear");
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField             // called when 'return' key pressed. return NO to ignore.
{
    return YES;
}

@end


你可能感兴趣的:(iOS UITextFiled 控件 限制其输入长度(手机号限长))