UITextField 設置左右視圖、文字距離及字符長度限制

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 ([textField isEqual:_passwordTF]) {

        if (range.location >=6) {

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

            returnNO;

        }

    }

    returnYES;

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章