xx-xx-xxx中的UITextField格式

时间:2011-10-10 07:00:22

标签: iphone objective-c ios uitextfield

我正在使用UITextField,我希望它的格式应该是xx-xx-xxx格式的数字。

任何帮助?

10 个答案:

答案 0 :(得分:59)

对于具有可变格式的电话号码,我们需要做得很好,这就是我写的。随意重用 - 首先我有一个方法来过滤格式化的字符串,#是一个数字,任何其他字符是某种“填充”,应该在用户到达需要的地方后方便地插入

NSMutableString *filteredPhoneStringFromStringWithFilter(NSString *string, NSString *filter)
{
    NSUInteger onOriginal = 0, onFilter = 0, onOutput = 0;
    char outputString[([filter length])];
    BOOL done = NO;

    while(onFilter < [filter length] && !done)
    {
        char filterChar = [filter characterAtIndex:onFilter];
        char originalChar = onOriginal >= string.length ? '\0' : [string characterAtIndex:onOriginal];
        switch (filterChar) {
            case '#':
                if(originalChar=='\0')
                {
                    // We have no more input numbers for the filter.  We're done.
                    done = YES;
                    break;
                }
                if(isdigit(originalChar))
                {
                    outputString[onOutput] = originalChar;
                    onOriginal++;
                    onFilter++;
                    onOutput++;
                }
                else
                {
                    onOriginal++;
                }
                break;
            default:
                // Any other character will automatically be inserted for the user as they type (spaces, - etc..) or deleted as they delete if there are more numbers to come.
                outputString[onOutput] = filterChar;
                onOutput++;
                onFilter++;
                if(originalChar == filterChar)
                    onOriginal++;
                break;
        }
    }
    outputString[onOutput] = '\0'; // Cap the output string
    return [NSString stringWithUTF8String:outputString];
}

现在他们可以通过填充删除,我修改了我应该更改范围内的字符。

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

    NSString *filter = @"(###) ### - ####";

    if(!filter) return YES; // No filter provided, allow anything

    NSString *changedString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    if(range.length == 1 && // Only do for single deletes
       string.length < range.length &&
       [[textField.text substringWithRange:range] rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"0123456789"]].location == NSNotFound)
    {
        // Something was deleted.  Delete past the previous number
        NSInteger location = changedString.length-1;
        if(location > 0)
        {
            for(; location > 0; location--)
            {
                if(isdigit([changedString characterAtIndex:location]))
                {
                    break;
                }
            }
            changedString = [changedString substringToIndex:location];
        }
    }

    textField.text = filteredPhoneStringFromStringWithFilter(changedString, filter);

    return NO;
}

这提供了一种非常干净的方式来强制用户输入特定格式的数字。

答案 1 :(得分:18)

尝试下面它会起作用

<强>目标C

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    int groupingSize = 2;
    if([string length] == 0) {
        groupingSize = 4;
    }
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init] ;
    NSString *separator = @"-";
    [formatter setGroupingSeparator:separator];
    [formatter setGroupingSize:groupingSize];
    [formatter setUsesGroupingSeparator:YES];
    [formatter setSecondaryGroupingSize:2];
    if (![string  isEqual: @""] && (textField.text != nil && textField.text.length > 0)) {
        NSString *num = textField.text;
        num = [num stringByReplacingOccurrencesOfString:separator withString:@""];
        NSString *str = [formatter stringFromNumber:[NSNumber numberWithDouble:[num doubleValue]]];
        textField.text = str;
    }
    return YES;
}

<强>夫特-3

extension ViewController: UITextFieldDelegate {

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        var groupSize = 2
        let separator = "-"
        if string.characters.count == 0 {
            groupSize = 4
        }
        let formatter = NumberFormatter()
        formatter.groupingSeparator = separator
        formatter.groupingSize = groupSize
        formatter.usesGroupingSeparator = true
        formatter.secondaryGroupingSize = 2
        if var number = textField.text, string != "" {
            number = number.replacingOccurrences(of: separator, with: "")
            if let doubleVal = Double(number) {
                let requiredString = formatter.string(from: NSNumber.init(value: doubleVal))
                textField.text = requiredString
            }

        }
        return true
    }
}

答案 2 :(得分:4)

textField:shouldChangeCharactersInRange:replacementString:内实施您的逻辑,这是一种委托方法。

答案 3 :(得分:2)

Swift类型的长方法但工作正常:iOS 8

的ViewController     //编程标记----- ----- ---- ----- ----- ---- ----- ----- ----

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    self.tfCardNumber.addTarget(self, action: "creditCardNumberFormatter:", forControlEvents: .EditingChanged)
    self.tfExpiryValue.addTarget(self, action: "creditCardExpiryFormatter:", forControlEvents: .EditingChanged)

}// end viewDidAppear

  func creditCardNumberFormatter(sender: AnyObject!) {

    // create object universally access global methods
    var objMethodInc = MethodInc(object: self)

    var formattedText = objMethodInc.formatCreditCard(self.tfCardNumber.text)

    if formattedText != self.tfCardNumber.text {

        self.tfCardNumber.text = formattedText

    }

    if countElements(self.tfCardNumber.text) == 19 {

        self.tfCardNumber.resignFirstResponder()
        self.tfExpiryValue.becomeFirstResponder()

    }


}// end creditCardNumberFormatter

func creditCardExpiryFormatter(sender: AnyObject!) {

    // create object universally access global methods
    var objMethodInc = MethodInc(object: self)

    var formattedText = objMethodInc.formatCreditCardExpiry(self.tfExpiryValue.text)

    if formattedText != self.tfExpiryValue.text {

        self.tfExpiryValue.text = formattedText

    }

}// end creditCardExpiryFormatter

// prgm mark --- ---- --- ----

// delegate for textfield

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange
    , replacementString string: String) -> Bool {

    if string == "" {
        return true
    }

    if textField == self.tfCardNumber {

        if countElements(self.tfCardNumber.text) > 18 {

            return false

        } else if textField == self.tfExpiryValue {

            if countElements(self.tfExpiryValue.text) > 4 {
                return false
            }

        }
    }

    return true

}// end textField

----------- INSIDE您的命令方法或文件--------

// prgm mark ----

func formatCreditCard(input: NSString) -> String {

    var input = self.trimSpecialCharacters(input);

    println("formatCreditCard input \(input)")

    var output : NSString?

    switch (input.length) {

    case 1: output = input
    case 2: output = input
    case 3: output = input
    case 4:
        var vs = input.substringToIndex(input.length)
        output = NSString(string: "\(vs)")
        break

    case 5: output = input
    case 6: output = input
    case 7: output = input
    case 8:
        var vs1 = input.substringToIndex(4)
        var vs2 = input.substringFromIndex(4)
        output = NSString(string: "\(vs1)-\(vs2)")
        break

    case 9: output = input
    case 10: output = input
    case 11: output = input

    case 12:
        var vs1 = input.substringToIndex(4)
        var vs2 = input.substringWithRange(NSMakeRange(4, 4))
        var vs3 = input.substringFromIndex(8)
        output = NSString(string: "\(vs1)-\(vs2)-\(vs3)")
        break

    case 13: output = input
    case 14: output = input
    case 15: output = input
    case 16:
        var vs1 = input.substringToIndex(4)
        var vs2 = input.substringWithRange(NSMakeRange(4, 4))
        var vs3 = input.substringWithRange(NSMakeRange(8, 4))
        var vs4 = input.substringFromIndex(12)
        output = NSString(string: "\(vs1)-\(vs2)-\(vs3)-\(vs4)")
        break
    default:
        output = input
        break

    }//  end switch

    println("formatCreditCard out \(output!)")

    return output!

}// end  formatCreditCard


// prgm mark ----

func formatCreditCardExpiry(input: NSString) -> String {

    var output : NSString?

    var input = self.trimSpecialCharacters(input);

    switch (input.length) {

    case 1: output = input
    case 2:
        var vs = input.substringToIndex(input.length)
        output = NSString(string: "\(vs)")
        break

    case 3: output = input
    case 4:
        var vs1 = input.substringToIndex(2)
        var vs2 = input.substringFromIndex(2)
        output = NSString(string: "\(vs1)/\(vs2)")
        break
    default:
        output = input
        break
    }

    return output!

}// end  formatCreditCardExpiry


func trimSpecialCharacters(input: NSString) -> NSString {

    var special = NSCharacterSet(charactersInString: "/+-() ")

    var comp = input.componentsSeparatedByCharactersInSet(special) as NSArray

    return comp.componentsJoinedByString("")

}//end trimSpecialCharacters

答案 4 :(得分:2)

在看了每个人的答案之后,我就采取了这个看法。

Swift 4

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let separator = "-"
    let filler = "X"
    if var number = textField.text, string != "" {
        number = number.replacingOccurrences(of: separator, with: "")
        number = number.replacingOccurrences(of: filler, with: "")
        if number.count == 10 { return false }
        number += string
        while number.count < 10 { number += "X" }
        number.insert("-", at: number.index(number.startIndex,
                                            offsetBy: 6))
        number.insert("-", at: number.index(number.startIndex,
                                            offsetBy: 3))
        textField.text = number
    }
    return false
}

答案 5 :(得分:2)

我的解决方案就是这样:

enter image description here

在您的textfield代理中实施:

func textFieldDidBeginEditing(_ textField: UITextField) {
        // When you start editing check if there is nothing, in that case add the entire mask
        if let text = textField.text, text == "" || text == "DD/MM/YYYY" {
            textField.text = "DD/MM/YYYY"
            textField.textColor = .lightGray
            textField.setCursor(position: text.count)
        }
    }

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    guard var number = textField.text else {
        return true
    }
    // If user try to delete, remove the char manually
    if string == "" {
        number.remove(at: number.index(number.startIndex, offsetBy: range.location))
    }
    // Remove all mask characters
    number = number.replacingOccurrences(of: "/", with: "")
    number = number.replacingOccurrences(of: "D", with: "")
    number = number.replacingOccurrences(of: "M", with: "")
    number = number.replacingOccurrences(of: "Y", with: "")

    // Set the position of the cursor
    var cursorPosition = number.count+1
    if string == "" {
        //if it's delete, just take the position given by the delegate
        cursorPosition = range.location
    } else {
        // If not, take into account the slash
        if cursorPosition > 2 && cursorPosition < 5 {
            cursorPosition += 1
        } else if cursorPosition > 4 {
            cursorPosition += 2
        }
    }
    // Stop editing if we have rich the max numbers
    if number.count == 8 { return false }
    // Readd all mask char
    number += string
    while number.count < 8 {
        if number.count < 2 {
            number += "D"
        } else if number.count < 4 {
            number += "M"
        } else {
            number += "Y"
        }
    }
    number.insert("/", at: number.index(number.startIndex, offsetBy: 4))
    number.insert("/", at: number.index(number.startIndex, offsetBy: 2))

    // Some styling
    let enteredTextAttribute = [NSForegroundColorAttributeName: UIColor.black, NSFontAttributeName: UIFont.systemFont(ofSize: 15)]
    let maskTextAttribute = [NSForegroundColorAttributeName: UIColor.lightGray, NSFontAttributeName: UIFont.systemFont(ofSize: 15)]

    let partOne = NSMutableAttributedString(string: String(number.prefix(cursorPosition)), attributes: enteredTextAttribute)
    let partTwo = NSMutableAttributedString(string: String(number.suffix(number.count-cursorPosition)), attributes: maskTextAttribute)

    let combination = NSMutableAttributedString()

    combination.append(partOne)
    combination.append(partTwo)

    textField.attributedText = combination
    textField.setCursor(position: cursorPosition)


    return false
}

func textFieldDidEndEditing(_ textField: UITextField) {
    if let text = textField.text, text != "" && text != "DD/MM/YYYY" {
        // Do something with your value
    } else {
        textField.text = ""
    }
}

那个小帮手作为扩展名:

extension UITextField {
    func setCursor(position: Int) {
        let position = self.position(from: beginningOfDocument, offset: position)!
        selectedTextRange = textRange(from: position, to: position)
    }
}

PS:当您尝试在编辑时移动光标

时,该实现中仍然存在错误

答案 6 :(得分:1)

您可以将textField所需的任何模式调用path到此方法中:

extension String {
    func applyPatternOnNumbers(pattern: String, replacmentCharacter: Character) -> String {
        var pureNumber = self.replacingOccurrences( of: "[^0-9]", with: "", options: .regularExpression)
        for index in 0 ..< pattern.count {
            guard index < pureNumber.count else { return pureNumber }
            let stringIndex = String.Index(encodedOffset: index)
            let patternCharacter = pattern[stringIndex]
            guard patternCharacter != replacmentCharacter else { continue }
            pureNumber.insert(patternCharacter, at: stringIndex)
        }
        return pureNumber
    }
}

示例:

 guard let text = textField.text else { return }
 textField.text = text.applyPatternOnNumbers(pattern: "##-##-###", replacmentCharacter: "#")

答案 7 :(得分:1)

经过对其中一个答案的一些自定义后,这对我有用。

extension String {
    func applyPatternOnNumbers(pattern: String, filler: Character) -> String {
        var pureNumber = self.replacingOccurrences( of: "[^0-9]", with: "", options: .regularExpression)
        // Insert filler for all occurances of filler in pattern
        for index in pattern.indices {
            print(pattern[index])
            let patternChar = pattern[index]
            if patternChar == filler {
                if index <= pureNumber.endIndex {
                    pureNumber.insert(filler, at: index)
                }
            }
        }
        return pureNumber
    }
}

用法:

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if(text == "\n") {
        textView.resignFirstResponder()
        return false
    } else if !text.isEmpty {
        guard let text = textView.text else { return true }
        textView.text = text.applyPatternOnNumbers(pattern: "###-##-####", filler: "-")
    }
    return true
}

答案 8 :(得分:0)

我的谷歌搜索告诉我你应该实施

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

来自UITextFieldDelegate协议。

有关详细信息,请查看this

答案 9 :(得分:0)

如果你希望它像日期一样,你就可以制作你自己的日期格式化程序:

NSDateFormatter *formatter;
NSString        *dateString;

formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy HH:mm"]; //or something in your own style

dateString = [formatter stringFromDate:[NSDate date]];

[formatter release];  // maybe; you might want to keep the formatter 
                      // if you're doing this a lot.

Getting Current Time in string in Custom format in objective c