在文本字段中自动完成 Twitter 用户名(可可)

发布于 2024-12-07 13:12:26 字数 508 浏览 1 评论 0原文

我正在查看 NSTokenField、NSTextField 和 NSTextView,但没有运气执行以下操作:

当您想发布一条新推文时,您可以开始在文本字段中写入,例如:

我正在编写一个 Twtitter 客户端, 咖啡,@pe

当您开始编写@ 时,我想帮助用户自动完成用户名,例如@peter。我有一个 NSArray,其用户名如下:

NSArray *usernames = [NSArray arrayWithObjects:@"@andreas", @"@clara", @"@jeena", @"@peter"]

应该做什么我要启用简单的自动完成功能吗?如果您也必须按 F5 或其他启动键,我会很高兴。我遇到的问题是,使用 NSTokenField 我不知道如何标记字符串,使用 NSTextField 仅当我在推文开头写入 @username 时才有效,而 NSTextView 似乎非常复杂,对于如此简单的操作来说太多了事物。

I was looking at NSTokenField, NSTextField and NSTextView with no luck to do the following:

I am writing a Twtitter client and when you want to twitter a new Tweet then you begin to write in a text field for example:

Going to make coffee, @pe

When you begin to write a @ then I would like to help the user to autocomplete the username for example @peter. I have a NSArray with the usernames like:

NSArray *usernames = [NSArray arrayWithObjects:@"@andreas", @"@clara", @"@jeena", @"@peter"]

What should I do to enable a simple autocompletation? I'd be happy if you would have to press F5 or something for starters too. The problem I am having is that with NSTokenField I don't know how I should tokenize the string, with NSTextField it only works when I write the @username at the beginning of the tweet and NSTextView seems really complicated and too much for such a simple thing.

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

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

发布评论

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

评论(1

空名 2024-12-14 13:12:26

最基本的实现涉及重写此方法......绝对不是最佳的,但您应该明白:

- (NSArray *) completionsForPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index {
    // this would be defined somewhere else, but just for example.. 
    NSArray *usernames = [NSArray arrayWithObjects:@"@andreas", @"@clara", @"@jeena", @"@peter"];

    NSMutableArray *matchedNames = [NSMutableArray array];
    NSString *toMatch = [[self string] substringWithRange:charRange];
    for(NSString *username in usernames) {
        [matchedNames addObject:username];
    }

    return matchedNames; // that's it. 
}

一旦您开始拥有大量数据,您将需要采用策略通过将单词存储到哈希中来预先执行搜索其中的部分文本(例如,“Hello”将被放入 4 个不同的数组中,内容放入 NSDictionary 键中,表示“H”、“He”、“Hel”、“Hell” .. 重复与你词典中的每一个单词。这样速度会快得多,

如果您想支持自动完成,只需在检测到控件中的文本发生更改时调用“complete:”方法即可。

The most basic implementation involves overriding this method... Definitely not optimal, but you should get the idea:

- (NSArray *) completionsForPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index {
    // this would be defined somewhere else, but just for example.. 
    NSArray *usernames = [NSArray arrayWithObjects:@"@andreas", @"@clara", @"@jeena", @"@peter"];

    NSMutableArray *matchedNames = [NSMutableArray array];
    NSString *toMatch = [[self string] substringWithRange:charRange];
    for(NSString *username in usernames) {
        [matchedNames addObject:username];
    }

    return matchedNames; // that's it. 
}

Once you start having a lot of data, you'll need to employ strategies to pre-do your searches by storing words into hashes with the partial pieces of text in them (like, "Hello" would be put into 4 different arrays stuff into NSDictionary keys for "H", "He", "Hel", "Hell" .. Repeat with every word in your Lexicon. Much quicker that way.

If you want to support auto-complete, just call the 'complete:' method when you detect text is changing in your control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文