ios – 如何设置UITextField的字母间距

我有一个应用程序,用户必须键入一个四位数的引脚代码.所有数字必须距离彼此设定的距离.

如果PinTextField是UIView的子类,是否有办法?我知道在ViewController中,您可以使用UITextFieldTextDidChangeNotification,并为每次更改设置归因文本.通知在UIView中似乎不起作用.

另外我想知道如果你想设置一个UITextField文本的字母间距,是不是比每个更新的属性字符串更简单的方法

正确间距:

间距错误

解决方法

这是最终为每一个变化而设定的核心
textField.addTarget(self,action: "textFieldDidChange",forControlEvents: .EditingChanged)

    func textFieldDidChange () {    
        let attributedString = NSMutableAttributedString(string: textField.text)
        attributedString.addAttribute(NSKernAttributeName,value: 5,range: NSMakeRange(0,count(textField.text)))
        attributedString.addAttribute(NSFontAttributeName,value: font,count(textField.text)))
        attributedString.addAttribute(NSForegroundColorAttributeName,value: UIColor.blackColor(),count(textField.text)))

        textField.attributedText = attributedString
    }

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

        if count(textField.text) < 4 {
            return true
            // Else if 4 and delete is allowed
        }else if count(string) == 0 {
            return true
            // Else limit reached
        }else{
            return false
        }
    }

然而,问题依然存在,因为不同的数字有不同的宽度,我只是回到为每个数字制作一个UITextField.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...