SWIFT:Nstextfield仅允许指定字符

jaxagkaj  于 2023-03-22  发布在  Swift
关注(0)|答案(3)|浏览(89)

我需要有人告诉我如何在Swift中允许nstexfield中的特定字符。例如,当用户尝试输入不在列表中的字符时,nstexfield不会在字段中显示该字符。.非常简单。有很多IOS示例,但找不到OSX示例。

brgchamk

brgchamk1#

首先向类中添加一个NSTextFieldDelegate ...然后
添加以下内容:

override func controlTextDidChange(obj: NSNotification) {
    let characterSet: NSCharacterSet = NSCharacterSet(charactersInString: " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789-_").invertedSet
    self.textField.stringValue = (self.textField.stringValue.componentsSeparatedByCharactersInSet(characterSet) as NSArray).componentsJoinedByString("")
}

你必须替换self.textfield与你自己的文本字段,你想控制。

SWIFT 4编辑

override func controlTextDidChange(_ obj: Notification) {
    let characterSet: NSCharacterSet = NSCharacterSet(charactersIn: " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789-_").inverted as NSCharacterSet
    self.textField.stringValue =  (self.textField.stringValue.components(separatedBy: characterSet as CharacterSet) as NSArray).componentsJoined(by: "")
}
ddarikpa

ddarikpa2#

对于Swift 3:

override func controlTextDidChange(_ notification: Notification) {
    if textField == notification.object as? NSTextField {
        let characterSet: CharacterSet = (CharacterSet(charactersIn: "0123456789").inverted as NSCharacterSet) as CharacterSet
        textField.stringValue = (textField.stringValue.components(separatedBy: characterSet) as NSArray).componentsJoined(by: "")
    }
}
6psbrbz9

6psbrbz93#

Swift 5.7:
如果您只想允许输入数字,请执行以下操作:

import Cocoa

class Main: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        textfield.delegate = self
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }
    var textfieldRecord = ""

}
extension Main: NSTextFieldDelegate {
    func controlTextDidChange(_ obj: Notification) {
        let x = (obj.object as! NSTextField).stringValue.trimmingCharacters(in: CharacterSet.decimalDigits)
        if x != "" {
            (obj.object as! NSTextField).stringValue = textfieldRecord
            __NSBeep()
        }
        textfieldRecord = (obj.object as! NSTextField).stringValue
    }
}

如果只允许输入指定的字符,可以执行以下操作:

import Cocoa

class Main: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        textfield.delegate = self
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }
    var textfieldRecord = ""
    var listOfAllowedCharacters = "1234567890-"
}
extension Main: NSTextFieldDelegate {
    func controlTextDidChange(_ obj: Notification) {
        let difference = Set((obj.object as! NSTextfield).stringValue).symmetricDifference(Set(textfieldRecord))
        if (obj.object as! NSTextfield).stringValue.count > textfieldRecord.count {
            if listOfAllowedCharacters.contains(difference) {
                textfieldRecord = (obj.object as! NSTextField).stringValue
            } else {
                (obj.object as! NSTextField).stringValue = textfieldRecord
                __NSBeep()
            }
        }
    }
}

希望这能帮到你

相关问题