在处理一个新的文本输入后,我正在尝试将NSAttributedString样式应用于UITextField。问题是,每当我替换文本时,光标在每次更改时都会跳到最末尾。下面是我目前使用的…方法
立即接收和显示文本更改(当我返回false并手动进行文本替换时,同样的问题也会出现)
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通知。这将触发样式设置。当文本发生更改时,我会使用某个(NSAttributedString) ()函数将其替换为格式化版本的格式(NSString)。
func textFieldTextDidChangeNotification(userInfo:NSDictionary) {
var attributedString:NSMutableAttributedString = NSMutableAttributedString(string: fullString)
attributedString = format(textField.text) as NSMutableAttributedString
textField.attributedText = attributedString
}
样式可以像这样很好地工作。但是,每次替换文本后,光标都会跳到字符串的末尾。如何关闭此行为或手动将光标移回开始编辑的位置?…或者,有没有更好的解决方案,在编辑时设置文本样式?
发布于 2014-10-10 01:29:14
执行此操作的方法是获取光标的位置,更新字段内容,然后将光标替换到其原始位置。我不确定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;
}
发布于 2016-05-04 22:46:30
感谢Objective-C中的代码@Stonz2。它的效果就像一个护身符!我在我的Swift项目中使用了它。Swift中的相同代码:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let positionOriginal = textField.beginningOfDocument
let cursorLocation = textField.position(from: positionOriginal, offset: (range.location + NSString(string: string).length))
/* MAKE YOUR CHANGES TO THE FIELD CONTENTS AS NEEDED HERE */
if let cursorLoc = cursorLocation {
textField.selectedTextRange = textField.textRange(from: cursorLoc, to: cursorLoc)
}
return false
}
发布于 2015-02-05 21:35:00
这是一个老问题,但我遇到了类似的问题,并通过以下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
它在我的应用程序的上下文中工作,希望对某些人有用!
https://stackoverflow.com/questions/26284271
复制相似问题