在 iPhone UITextView 上打字(如 Twitter)时建议 # 个标签
我正在构建一个使用主题标签的应用程序,例如 Twitter 或 Tweetbot。当您输入消息时,如果您输入主题标签符号,我会建议与您当前输入的标签相匹配的标签。
我已经弄清楚如何让 UITableView 出现并显示主题标签列表,但我不知道如何执行以下操作:
- 获取当前单词的
NSRange
输入, - 看看该范围的格式是否像主题标签(
NSRegularExpression @"#\\w\\w*"
) - (从这里开始,我已经弄清楚了搜索匹配的代码主题标签和显示它们在 UITableView 中)
任何人都可以帮助我完成步骤 1 和 2 吗?我一直在考虑使用 textViewDidChange:
,但我担心如果我每次字符更改时都不断运行方法,应用程序的性能可能会受到影响。
谢谢!
I'd building an app that uses hashtags, like Twitter or Tweetbot. When you're typing a message, if you type the hashtag symbol, I'd like to suggest tags that match the current one you're typing.
I've already figured out how to get the UITableView to appear and show a list of hashtags, but what I can't figure out is how to do the following:
- Get the
NSRange
of the current word being typed, - See if that range is formatted like a hashtag (
NSRegularExpression @"#\\w\\w*"
) - (From here on out, I've got the code figured out to search for matching hashtags and show them in the UITableView)
Can anyone help me with steps 1 and 2? I've been thinking about using textViewDidChange:
, but I'm concerned that the app's performance might suffer if I'm constantly running methods every time the characters change.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想通了!我最终使用了
textViewDidChange:
和textViewDidChangeSelection:
方法。为了获取当前键入的主题标签的
NSRange
,我对文本字符串中的NSRegularExpression
匹配运行了for
循环。从那里,我使用 NSLocationInRange 来查找当前光标位置是否与任何主题标签相交。代码如下:
StringChecker
类是我编写的自定义类,它只有解析字符串的类方法。我将StringChecker
创建为一个类,因为这些方法在应用程序的多个位置使用。方法如下:I figured it out! I wound up using the
textViewDidChange:
andtextViewDidChangeSelection:
methods.To get the
NSRange
of the current hashtag being typed, I ran afor
loop over theNSRegularExpression
matches in the text string. From there, I usedNSLocationInRange
to find out if the current cursor position intersected any of the hashtags.Here's the code:
The
StringChecker
class is a custom one that I wrote, it just has class methods that parse the strings. I madeStringChecker
a class because the methods are used in several places in the app. Here's the method:我想出的另一种方法如下。
在
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
函数中,我为正在键入的 # 放置了一个监听器,它开始记录下面的字符散列,直到用户键入一个空格,此时它会重置。如果
BOOL reportingHashTag
设置为YES
,我会将包含主题标签文本的substring
传递给一个搜索预先填充的主题标签数组的函数。如果存在匹配项,则会将该条目添加到经过过滤的主题标签数组中,并使用该数组动态填充tableview
。最后一步是当用户单击表中的条目时插入哈希标签。
只是不要忘记在用户退格中间哈希标记时清除变量。
Another way I figured out to do this is as follows.
In the
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
function I put a listener for a # being typed which begins recording the characters following the hash until the user types a space at which time it resets.If the
BOOL recordingHashTag
is set toYES
I pass thesubstring
containing the hashtag text to a function which searches a pre populated array of hashtags. If there is a match it adds that entry to a filtered array of hashtags which it uses to populate thetableview
on the fly.The final step is to insert the hash tag when the user clicks on the entry in the table.
Just don't forget to clear out your variables when a user backspaces mid hash tag.