格式UITextField文本没有光标移动到最后

我正在尝试在处理新的文本条目后,通过按键击键将NSAttributedString样式应用于UITextField 。 问题是,当我更换文本时,光标会在每次更改时跳到最后。 这是我目前的做法…

立即接收并显示文本更改(当我返回false并手动进行文本replace时,也是同样的问题)

func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool { let range:NSRange = NSRange(location: range.location, length: range.length) let newString = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string); return true } 

我订阅了UITextFieldTextDidChangeNotification通知。 这触发了样式。 当文本被改变时,我用一些format()函数replace它( NSString )与格式化的版本( NSAttributedString )。

 func textFieldTextDidChangeNotification(userInfo:NSDictionary) { var attributedString:NSMutableAttributedString = NSMutableAttributedString(string: fullString) attributedString = format(textField.text) as NSMutableAttributedString textField.attributedText = attributedString } 

造型工作正常。 但是,在每次文本replace之后,光标会跳转到string的末尾。 如何closures此行为或手动将光标移回到开始编辑的位置? …还是有更好的解决scheme来编辑​​文本的样式?

使其工作的方法是获取光标位置,更新字段内容,然后将光标replace为原始位置。 我不确定在Swift中的确切等价物,但是下面是我将如何在Obj-C中完成的。

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { UITextPosition *beginning = textField.beginningOfDocument; UITextPosition *cursorLocation = [textField positionFromPosition:beginning offset:(range.location + string.length)]; textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string]; /* MAKE YOUR CHANGES TO THE FIELD CONTENTS AS NEEDED HERE */ // cursorLocation will be (null) if you're inputting text at the end of the string // if already at the end, no need to change location as it will default to end anyway if(cursorLocation) { // set start/end location to same spot so that nothing is highlighted [textField setSelectedTextRange:[textField textRangeFromPosition:cursorLocation toPosition:cursorLocation]]; } return NO; } 

这是一个古老的问题,但我有一个类似的问题,并解决了与以下Swift:

 // store the current cursor position as a range var preAttributedRange: NSRange = textField.selectedRange // apply attributed string var attributedString:NSMutableAttributedString = NSMutableAttributedString(string: fullString) attributedString = format(textField.text) as NSMutableAttributedString textField.attributedText = attributedString // reapply the range textField.selectedRange = preAttributedRange 

它在我的应用程序的上下文中工作,希望对某人有用!

感谢@ Stonz2的Objective-C中的代码。 它就像一个魅力! 我在我的Swift项目中使用它。 Swift中的相同代码:

 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { let positionOriginal = textField.beginningOfDocument let cursorLocation = textField.positionFromPosition(positionOriginal, offset: (range.location + NSString(string: string).length)) /* MAKE YOUR CHANGES TO THE FIELD CONTENTS AS NEEDED HERE */ if(cursorLocation != nil) { textField.selectedTextRange = textField.textRangeFromPosition(cursorLocation!, toPosition: cursorLocation!) } return false } 

这是一个适用于swift的代码。享受它!

  func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { let beginnig: UITextPosition = textField.beginningOfDocument if let txt = textField.text { textField.text = NSString(string: txt).stringByReplacingCharactersInRange(range, withString: string) } if let cursorLocation: UITextPosition = textField.positionFromPosition(beginnig, offset: (range.location + string.characters.count) ) { textField.selectedTextRange = textField.textRangeFromPosition(cursorLocation, toPosition: cursorLocation) } return false } 

请注意,最后的“如果让”应该始终停留在代码的末尾。