首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >设置UITextField文本格式,而不将光标移动到末尾

设置UITextField文本格式,而不将光标移动到末尾
EN

Stack Overflow用户
提问于 2014-10-10 01:16:02
回答 6查看 15.7K关注 0票数 33

在处理一个新的文本输入后,我正在尝试将NSAttributedString样式应用于UITextField。问题是,每当我替换文本时,光标在每次更改时都会跳到最末尾。下面是我目前使用的…方法

立即接收和显示文本更改(当我返回false并手动进行文本替换时,同样的问题也会出现)

代码语言:javascript
运行
复制
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)。

代码语言:javascript
运行
复制
func textFieldTextDidChangeNotification(userInfo:NSDictionary) {
    var attributedString:NSMutableAttributedString = NSMutableAttributedString(string: fullString)
    attributedString = format(textField.text) as NSMutableAttributedString
    textField.attributedText = attributedString
}

样式可以像这样很好地工作。但是,每次替换文本后,光标都会跳到字符串的末尾。如何关闭此行为或手动将光标移回开始编辑的位置?…或者,有没有更好的解决方案,在编辑时设置文本样式?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2014-10-10 01:29:14

执行此操作的方法是获取光标的位置,更新字段内容,然后将光标替换到其原始位置。我不确定Swift中的确切等价物,但以下是我将如何在Obj-C中做到这一点。

代码语言:javascript
运行
复制
- (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;
}
票数 49
EN

Stack Overflow用户

发布于 2016-05-04 22:46:30

感谢Objective-C中的代码@Stonz2。它的效果就像一个护身符!我在我的Swift项目中使用了它。Swift中的相同代码:

代码语言:javascript
运行
复制
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 
}
票数 14
EN

Stack Overflow用户

发布于 2015-02-05 21:35:00

这是一个老问题,但我遇到了类似的问题,并通过以下Swift解决了它:

代码语言:javascript
运行
复制
// 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

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

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26284271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档