How to set letter spacing of UITextField

前端 未结 7 909
[愿得一人]
[愿得一人] 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:22

    This is working fine in Swift 2.2. Hope this will help you for letter spacing in text field

    override func viewDidLoad() {
     // Do any additional setup after loading the view.
            NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(SignupVC.limitTextField(_:)), name: "UITextFieldTextDidChangeNotification", object: txtContactNumber)
    }
     func limitTextField(Notif:NSNotification) {
    
        let limit=10;
    
        let attributedString = NSMutableAttributedString(string: txtContactNumber.text!)
        attributedString.addAttribute(NSKernAttributeName, value: 7, range: NSMakeRange(0, (txtContactNumber.text?.characters.count)!))
       // attributedString.addAttribute(NSFontAttributeName, value: font, range: NSMakeRange(0, count(textField.text)))
        attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: NSMakeRange(0,(txtContactNumber.text?.characters.count)!))
    
        txtContactNumber.attributedText = attributedString
        if(txtContactNumber.text?.characters.count>limit)
        {
            txtContactNumber.text=txtContactNumber.text?.substringToIndex(limit)
        }
    }
    

提交回复
热议问题