以编程方式在 UiTextField 中移动光标

发布于 2024-09-07 20:03:38 字数 380 浏览 3 评论 0原文

我需要调整 UiTextField 行为,以便可以以编程方式插入一些星号并将光标移动到字符串的开头; 我尝试了“粘贴”技巧(http:// /dev.ragfield.com/2009/09/insert-text-at-current-cursor-location.html),但光标移动到末尾。 我试图达到的效果是这样的: 开始时:


然后用户插入字符 妈的 佛**** 富*** 等等...

这是可能的还是我必须做的调整不值得付出努力?

谢谢

-G-

I need to tweak UiTextField behavior so that I can programmatically insert some asterisks and move the cursor at the beginning of the string;
I tried the "paste" trick (http://dev.ragfield.com/2009/09/insert-text-at-current-cursor-location.html), but the cursor is moved at the end.
The effect I try to achieve is like this:
At the start:


then the user insert characters
f*****
fo****
foo***
etc...

Is this possible or the tweaks I've to do don't deserve the effort?

Thanx

-G-

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪花飘飘的天空 2024-09-14 20:03:38

更新:

在 iOS5 及更高版本中可以执行此操作,因为 UITextField 和 UITextView 符合 UITextInput 协议。请看一下:“ 我可以吗在 UITextField 中选择特定的文本块?”作为示例:)。

希望它有帮助


旧答案:(iOS4.x 及以下版本):

没有办法使用公共 API 来完成此任务(据我所知)。
不过我发现了一些私有方法:(可能是未记录的方法,因为它们没有私有方法通常具有的下划线前缀)

selectionRange
setSelectionRange:

下面的代码工作正常(至少在模拟器 4.3 中)并且我正在使用 KVC,因此我可以避免烦人的警告。 (selfUITextField 的子类的实例)

- (void) moveToRight{

    NSRange selectedRange = [[self valueForKey:@"selectionRange"] rangeValue];
    if (selectedRange.location != NSNotFound) {
        if (selectedRange.length > 0) {
            selectedRange.location = NSMaxRange(selectedRange);
            selectedRange.length = 0;
        }else if (selectedRange.location < self.text.length) {
            selectedRange.location += 1;
        }
        [self setValue:[NSValue valueWithRange:selectedRange] forKey:@"selectionRange"];
    }
}
- (void) moveToLeft{

    NSRange selectedRange = [[self valueForKey:@"selectionRange"] rangeValue];
    if (selectedRange.location != NSNotFound) {
        if (selectedRange.length > 0) {
            selectedRange.length = 0;
        }else if (selectedRange.location > 0) {
            selectedRange.location -= 1;
        }
        [self setValue:[NSValue valueWithRange:selectedRange] forKey:@"selectionRange"];
    }
}

由于这些不是公共 API,请自行承担使用风险,我不知道它们是否会通过 Apple 的审核。

顺便提一句:
我发现他们使用:

#import "/usr/include/objc/objc-runtime.h"

unsigned int methodCount = 0;
Method *mlist = class_copyMethodList([UITextField class], &methodCount);
for (int i = 0; i < methodCount; ++i){
    NSLog(@"%@", NSStringFromSelector(method_getName(mlist[i])));
}

UPDATE:

In iOS5 and above is possible to do this because UITextField and UITextView conform to UITextInput protocol. Please take a look at this: " Can I select a specific block of text in a UITextField? " for an example :).

Hope it helps


OLD ANSWER: (iOS4.x and below):

There is no way to accomplish this using public APIs (Not that I know).
However I have found a couple of private methods: (maybe undocumented methods because they don't have the underscore prefix that private methods usually have)

selectionRange
setSelectionRange:

Below code work fine (at least in the simulator 4.3) and I am using KVC so I can avoid annoying warnings. (self is a instance of a subclass of UITextField)

- (void) moveToRight{

    NSRange selectedRange = [[self valueForKey:@"selectionRange"] rangeValue];
    if (selectedRange.location != NSNotFound) {
        if (selectedRange.length > 0) {
            selectedRange.location = NSMaxRange(selectedRange);
            selectedRange.length = 0;
        }else if (selectedRange.location < self.text.length) {
            selectedRange.location += 1;
        }
        [self setValue:[NSValue valueWithRange:selectedRange] forKey:@"selectionRange"];
    }
}
- (void) moveToLeft{

    NSRange selectedRange = [[self valueForKey:@"selectionRange"] rangeValue];
    if (selectedRange.location != NSNotFound) {
        if (selectedRange.length > 0) {
            selectedRange.length = 0;
        }else if (selectedRange.location > 0) {
            selectedRange.location -= 1;
        }
        [self setValue:[NSValue valueWithRange:selectedRange] forKey:@"selectionRange"];
    }
}

Since these are not public APIs, use at your own risk, I don't know if they will pass Apple's review.

BTW:
I found them using:

#import "/usr/include/objc/objc-runtime.h"

unsigned int methodCount = 0;
Method *mlist = class_copyMethodList([UITextField class], &methodCount);
for (int i = 0; i < methodCount; ++i){
    NSLog(@"%@", NSStringFromSelector(method_getName(mlist[i])));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文