UITextField 设置左右视图、文字距离及字符长度限制

来源:互联网 发布:毒蛇 知乎 编辑:程序博客网 时间:2024/04/29 07:40

UITextField 是iOS开发中的一个常用控件。并伴有左右视图的提示。 比如一个登陆界面, 需要我们输入用户名及密码, 这是多数app的常见模式了。  这样的界面, 左边, 可以是一个userName和password的文字或者图标提示, 右边则最常见的是获取验证码及让textField 安全输入或者可见输入。这种情况, 我们是去创建一堆控件进行提示和显示?  虽然这样可以获得相同的效果, 但是却是件费力不讨好的事情, 那么我们现在就来看看UITextField 自带的简单的设置左右视图。


1.设置UITextField输入框的左右视图

这样, 我们先写一个类继承自UITextField, 然后在这个类中, 重写UITextField中设置左右视图的某些方法, 如下:

设置textField的左右视图的显示位置

//左视图的位置和大小

-(CGRect)leftViewRectForBounds:(CGRect)bounds{

    CGRect leftRect = [superleftViewRectForBounds:bounds];

    leftRect.origin.x =10;

    return leftRect;

}


//右视图显示的位置和大小

-(CGRect)rightViewRectForBounds:(CGRect)bounds{

    CGRect rightRect =CGRectZero;

    rightRect.origin.x = bounds.size.width - 65;

    rightRect.size.height =25;

    rightRect.origin.y = (bounds.size.height - rightRect.size.height)/2;

    rightRect.size.width =55;

    return rightRect;

}


//可输入的字符的区域

-(CGRect)textRectForBounds:(CGRect)bounds{

    CGRect textRect = [supertextRectForBounds:bounds];

    if (self.leftView ==nil) {

        returnCGRectInset(textRect,10,0);

    }

    CGFloat offset =40 - textRect.origin.x;

    textRect.origin.x =40;

    textRect.size.width = textRect.size.width - offset - 10;

    return textRect;

}


//编辑显示的区域

-(CGRect)editingRectForBounds:(CGRect)bounds{

    CGRect textRect = [supereditingRectForBounds:bounds];

    if (self.leftView ==nil) {

        returnCGRectInset(textRect,10,0);

    }

    CGFloat offset =40 - textRect.origin.x;

    textRect.origin.x =40;

    textRect.size.width = textRect.size.width - offset - 10;

    return textRect;

}


设置好以上对视图,和显示区域的设置之后, 我们来看看怎么进行使用:


//设置UITextField支持左右视图模式

_userNameTF.leftViewMode =_passwordTF.leftViewMode =UITextFieldViewModeAlways;


//设置左视图

    UIImageView *usernameLeft = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"L_user_insert"]];

    usernameLeft.frame =CGRectMake(20,0,22,22);

    _userNameTF.leftView = usernameLeft;

    

    pwdLeftView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"L_pwd_insert"]];

    pwdLeftView.frame =CGRectMake(20,0,22,22);

    _passwordTF.leftView =pwdLeftView;    

    _passwordTF.placeholder =@"请输入服务密码";




2.限制用户输入字符的长度

在写程序的过程中, 我们往往会限制用户输入的字符的长度, 不能让他们无限输下去。 比如: 一个用户名, 一般都会规定在多少字符之内, 比如手机号,我们规定最长只能输入11位, 下面我们就来看看限制textField输入长度的解决方法


一般我们在限制输入的字符个数之前, 我们需要知道用户现在到底输入了多少个字符了 , 我们根据用户输入的情况, 来做进一步的限制, 使用UITextField的 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 方法(此方法在用户输入的过程中, 只要输入的字符有所变化, 不管是删除使之变短, 还是输入使之变长, 都会调用此方法)获取输入的动态信息,并进行判断:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    if ([textFieldisEqual:_userNameTF]) {

        if (range.location >=11) {

            textField.text = [textField.textsubstringToIndex:11];

            returnNO;

        }

    } elseif ([textFieldisEqual:_passwordTF]) {

        if (range.location >=6) {

            textField.text = [textField.textsubstringToIndex:6];

            returnNO;

        }

    }

    returnYES;

}


1 0