How to set letter spacing of UITextField

前端 未结 7 973
[愿得一人]
[愿得一人] 2021-01-30 12:55

I have an app in which the user has to type a four digit pin code. All digits have to be at a set distance from each other.

Is there a way to do this if the PinTex

7条回答
  •  青春惊慌失措
    2021-01-30 13:29

    This is what eventually worked to set the kern for every change

        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, range: NSMakeRange(0, count(textField.text)))
            attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: NSMakeRange(0, 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
            }
        }
    

    The problem however remains because different numbers have different widths, I will just resort back to making a UITextField for every digit.

提交回复
热议问题