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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值