iOS UITextField總結(jié)

修改光標(biāo)顏色、placeholder 顏色,手機(jī)號(hào)輸入格式化

  • 修改光標(biāo)顏色
    //修改textField光標(biāo)顏色,
    [textField setTintColor:[UIColor redColor]];
    //設(shè)置UITextField的光標(biāo)顏色
    // [[UITextField appearance] setTintColor:[UIColor redColor];
    
  • 修改placeholder 顏色

通過(guò)設(shè)置UITextField的attributedPlaceholder屬性,修改它的顏色 。因?yàn)榻?jīng)常用到,創(chuàng)建UITextField 的分類,將其封裝。

/** 設(shè)置textfield的placeholder及其顏色  */
-(void)dh_setPlaceholder:(NSString *)placeholder andPlaceholderColor:(UIColor *)placeholderColor {
    NSDictionary *attributes = @{
                                 NSForegroundColorAttributeName:placeholderColor,
                                 NSFontAttributeName:self.font
                                 };
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:placeholder attributes:attributes];
    self.attributedPlaceholder = attrString;
}
  • 手機(jī)號(hào)輸入格式化

    在用戶輸入電話號(hào)碼時(shí),自動(dòng)將其格式化為123 4567 8910。

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

    把textField中位置為range的字符串替換為string字符串;此函數(shù)在textField內(nèi)容被修改時(shí)調(diào)用;使用這個(gè)方法來(lái)驗(yàn)證使用時(shí)用戶輸入的類型。

    @param textField textField

    @param range UITextField控件中光標(biāo)選中的字符串,即被替換的字符串;range.length為0時(shí),表示在位置range.location插入string。

    @param string 替換字符串;string.length為0時(shí),表示刪除。

    @return YES,表示修改生效;NO,表示不做修改,textField的內(nèi)容不變。

    實(shí)現(xiàn)代碼(ps:一下代碼均實(shí)現(xiàn)在UITextField的分類中):

    -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
        //在輸入或刪除前的光標(biāo)位置
        NSRange currentRange = [self cursorPosition];
        //輸入或刪除前的內(nèi)容
        NSString *previousContent = textField.text;
        // 限制只能輸入數(shù)字
        NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
        string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
        if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
            return NO;
        }
        //把textField中位置為range的字符串替換為string字符串
        NSString *handleText = [previousContent stringByReplacingCharactersInRange:range withString:string];
        //純數(shù)字的字符串
        NSString *onlyDigitStr = [handleText stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSMutableString *temMstr = [NSMutableString new];
        for (NSInteger i = 0; i < onlyDigitStr.length; i++) {
            if (i == 3 || i == 7) {
                [temMstr appendString:@" "];
            }
            unichar digitchar = [onlyDigitStr characterAtIndex:i];
            [temMstr appendString:[NSString stringWithCharacters:&digitchar length:1]];
        }
        if (temMstr.length >= 14) {
            return NO;
        }
        [textField setText:temMstr];
    /*
        //不做任何處理時(shí),顯示的內(nèi)容
        NSString *text = [previousContent stringByReplacingCharactersInRange:range withString:string];
        text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSMutableString *temString = [NSMutableString stringWithString:text];
        [temString insertString:@" " atIndex:0];
        text = temString;
        NSString *newString = @"";
        while (text.length > 0) {
            NSString *subString = [text substringToIndex:MIN(text.length, 4)];
            newString = [newString stringByAppendingString:subString];
            if (subString.length == 4) {
                newString = [newString stringByAppendingString:@" "];
            }
            text = [text substringFromIndex:MIN(text.length, 4)];
        }
        newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
        if (newString.length >= 14) {
            return NO;
        }
        [textField setText:newString];
    */
        if (string.length == 0) {
            //刪除
            currentRange.location --;
            if (currentRange.location == 4 || currentRange.location == 9) {
                currentRange.location --;
            }
        }
        else {
            currentRange.location += string.length;
            if (currentRange.location == 4 || currentRange.location == 9) {
                currentRange.location ++;
            }
        }
        [self setCursorPosition:currentRange];
        return NO;
    }
    /**  光標(biāo)位置  */
    - (NSRange)cursorPosition {
        UITextPosition* beginning = self.beginningOfDocument;
        UITextRange* selectedRange = self.selectedTextRange;
        UITextPosition* selectionStart = selectedRange.start;
        UITextPosition* selectionEnd = selectedRange.end;
        const NSInteger location = [self offsetFromPosition:beginning toPosition:selectionStart];
        const NSInteger length = [self offsetFromPosition:selectionStart toPosition:selectionEnd];
        return NSMakeRange(location, length);
    }
    /**  設(shè)置光標(biāo)位置  */
    - (void)setCursorPosition:(NSRange) range {
        UITextPosition* beginning = self.beginningOfDocument;
        UITextPosition* startPosition = [self positionFromPosition:beginning offset:range.location];
        UITextPosition* endPosition = [self positionFromPosition:beginning offset:range.location + range.length];
        UITextRange* cursorPosition = [self textRangeFromPosition:startPosition toPosition:endPosition];
        [self setSelectedTextRange:cursorPosition];
    }
    
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容