Right aligned UITextField spacebar does not advance cursor in iOS 7

前端 未结 14 1683
执笔经年
执笔经年 2020-12-07 23:04

In my iPad app, I noticed different behavior between iOS 6 and iOS 7 with UITextFields.

I create the UITextField as follows:

UIButton *theButton = (U         


        
相关标签:
14条回答
  • 2020-12-07 23:46

    Transformed triazotan's answer into Swift3.

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool{
    
        if (range.location == textField.text?.characters.count && string == " ") {
            let noBreakSpace: Character = "\u{00a0}"
            textField.text = textField.text?.append(noBreakSpace)
            return false
        }
        return true
    }
    
    0 讨论(0)
  • 2020-12-07 23:47

    Here is Swift 3 from @Jack Song's answer

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if (textField == self.textfield) {
            let oldString = textField.text!
            let newStart = oldString.index(oldString.startIndex, offsetBy: range.location)
            let newEnd = oldString.index(oldString.startIndex, offsetBy: range.location + range.length)
            let newString = oldString.replacingCharacters(in: newStart..<newEnd, with: string)
            textField.text = newString.replacingOccurrences(of: " ", with: "\u{00a0}")
            return false;
        } else {
            return true;
        }
    }
    
    0 讨论(0)
提交回复
热议问题