格式化UITextField文本而不将光标移动到最后 [英] Format UITextField text without having cursor move to the end

查看:174
本文介绍了格式化UITextField文本而不将光标移动到最后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to apply NSAttributedString styles to a UITextField after processing a new text entry, keystroke by keystroke. The problem is that any time I replace the text the cursor will jump the very end on each change. Here's my current approach …

立即接收并显示文本更改(同样的问题适用于我返回false 并手动执行文本替换)

Receive and display text change immediately (same issue applies when I return false and do the text replacement manually)

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 )。

I subscribed to the UITextFieldTextDidChangeNotification notification. This triggers the styling. When the text is changed I replace it (NSString) with the formatted version (NSAttributedString) using some format() function.

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

样式工作正常。但是,在每次文本替换后,光标会跳转到字符串的末尾。如何关闭此行为或手动将光标移回到开始编辑的位置? ...或者是否有更好的解决方案来在编辑时设置文本样式?

Styling works fine like this. However after each text replacement the cursor jumps to the end of the string. How can I turn off this behaviour or manually move the cursor back where it started the edit? … or is there a better solution to style the text while editing?

推荐答案

使这项工作的方法是抓住位置的光标,更新字段内容,然后将光标替换为其原始位置。我不确定Swift中的确切等价物,但以下是我在Obj-C中的操作方法。

The way to make this work is to grab the location of the cursor, update the field contents, and then replace the cursor to its original position. I'm not sure of the exact equivalent in Swift, but the following is how I would do it in 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;
}

这篇关于格式化UITextField文本而不将光标移动到最后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆