在 iPhone UITextView 上打字(如 Twitter)时建议 # 个标签

发布于 2024-12-18 02:26:41 字数 447 浏览 1 评论 0原文

我正在构建一个使用主题标签的应用程序,例如 Twitter 或 Tweetbot。当您输入消息时,如果您输入主题标签符号,我会建议与您当前输入的标签相匹配的标签。

我已经弄清楚如何让 UITableView 出现并显示主题标签列表,但我不知道如何执行以下操作:

  1. 获取当前单词的 NSRange输入,
  2. 看看该范围的格式是否像主题标签(NSRegularExpression @"#\\w\\w*"
  3. (从这里开始,我已经弄清楚了搜索匹配的代码主题标签和显示它们在 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:

  1. Get the NSRange of the current word being typed,
  2. See if that range is formatted like a hashtag (NSRegularExpression @"#\\w\\w*")
  3. (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 技术交流群。

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

发布评论

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

评论(2

分開簡單 2024-12-25 02:26:41

我想通了!我最终使用了 textViewDidChange:textViewDidChangeSelection: 方法。

为了获取当前键入的主题标签的 NSRange,我对文本字符串中的 NSRegularExpression 匹配运行了 for 循环。从那里,我使用 NSLocationInRange 来查找当前光标位置是否与任何主题标签相交。

代码如下:

//Get the ranges of current hashtags
NSArray *hashtagRanges = [StringChecker rangesOfHashtagsInString:textView.text];
NSTextCheckingResult *currentHashtag;

if ([hashtagRanges count] >0)
{
    //List the ranges of all the hashtags
    for (int i = 0; i<[hashtagRanges count]; i++) 
    {
        NSTextCheckingResult *hashtag = [hashtagRanges objectAtIndex:i];
        //Check if the currentRange intersects the hashtag
        //Have to add an extra space to the range for if you're at the end of a hashtag. (since NSLocationInRange uses a < instead of <=)
        NSRange currentlyTypingHashtagRange = NSMakeRange(hashtag.range.location, hashtag.range.length + 1);
        if (NSLocationInRange(currentRange.location, currentlyTypingHashtagRange))
        {
            //If the cursor is over the hashtag, then snag that hashtag for matching purposes.
            currentHashtag = hashtag;
        }
    }

    if (currentHashtag){
        //If we found one hashtag that we're currently editing

        //Display the hashtag suggester, feed it the current hashtag for matching.
        [self showTagTable];

        //Get the current list of hashtags into an array
        NSFetchRequest *hashtagRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *tagEntityDescription = [NSEntityDescription entityForName:@"Tags" 
                                                                inManagedObjectContext:self.note.managedObjectContext];
        [hashtagRequest setEntity:tagEntityDescription];
        NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"dateLastUsed" 
                                                                         ascending:YES];
        NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
        [hashtagRequest setSortDescriptors:sortDescriptors];

        NSPredicate *tagPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", [noteTextView.text substringWithRange:currentHashtag.range]];
        [hashtagRequest setPredicate:tagPredicate];

        tagsToDisplay = (NSMutableArray *)[self.note.managedObjectContext executeFetchRequest:hashtagRequest error:nil];
        [tagListTable reloadData];

        //If there are no matching hashtags, then let's hide the tag table.
        if ([tagsToDisplay count] == 0) 
        {
            [self hideTagTable];
            return;
        }

    }

StringChecker 类是我编写的自定义类,它只有解析字符串的类方法。我将 StringChecker 创建为一个类,因为这些方法在应用程序的多个位置使用。方法如下:

    #pragma mark - Hashtag Methods
+(NSArray *)rangesOfHashtagsInString:(NSString *)string {
    NSRegularExpression *hashtagDetector = [[NSRegularExpression alloc] initWithPattern:@"#\\w\\w*" 
                                                                                options:NSRegularExpressionCaseInsensitive 
                                                                                  error:nil];    
    NSArray *hashtagRanges = [hashtagDetector matchesInString:string
                                                      options:NSMatchingWithoutAnchoringBounds
                                                        range:NSMakeRange(0, string.length)];
    return hashtagRanges;
}

+(NSUInteger)numberOfHashtagsInString:(NSString *)string {
    NSRegularExpression *hashtagDetector = [[NSRegularExpression alloc] initWithPattern:@"#\\w\\w*" 
                                                                                options:NSRegularExpressionCaseInsensitive 
                                                                                  error:nil];
    NSUInteger numberOfHashtags = [hashtagDetector numberOfMatchesInString:string
                                                                   options:NSRegularExpressionCaseInsensitive
                                                                     range:NSMakeRange(0, string.length)];
    return numberOfHashtags;
}

I figured it out! I wound up using the textViewDidChange: and textViewDidChangeSelection: methods.

To get the NSRange of the current hashtag being typed, I ran a for loop over the NSRegularExpression matches in the text string. From there, I used NSLocationInRange to find out if the current cursor position intersected any of the hashtags.

Here's the code:

//Get the ranges of current hashtags
NSArray *hashtagRanges = [StringChecker rangesOfHashtagsInString:textView.text];
NSTextCheckingResult *currentHashtag;

if ([hashtagRanges count] >0)
{
    //List the ranges of all the hashtags
    for (int i = 0; i<[hashtagRanges count]; i++) 
    {
        NSTextCheckingResult *hashtag = [hashtagRanges objectAtIndex:i];
        //Check if the currentRange intersects the hashtag
        //Have to add an extra space to the range for if you're at the end of a hashtag. (since NSLocationInRange uses a < instead of <=)
        NSRange currentlyTypingHashtagRange = NSMakeRange(hashtag.range.location, hashtag.range.length + 1);
        if (NSLocationInRange(currentRange.location, currentlyTypingHashtagRange))
        {
            //If the cursor is over the hashtag, then snag that hashtag for matching purposes.
            currentHashtag = hashtag;
        }
    }

    if (currentHashtag){
        //If we found one hashtag that we're currently editing

        //Display the hashtag suggester, feed it the current hashtag for matching.
        [self showTagTable];

        //Get the current list of hashtags into an array
        NSFetchRequest *hashtagRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *tagEntityDescription = [NSEntityDescription entityForName:@"Tags" 
                                                                inManagedObjectContext:self.note.managedObjectContext];
        [hashtagRequest setEntity:tagEntityDescription];
        NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"dateLastUsed" 
                                                                         ascending:YES];
        NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
        [hashtagRequest setSortDescriptors:sortDescriptors];

        NSPredicate *tagPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", [noteTextView.text substringWithRange:currentHashtag.range]];
        [hashtagRequest setPredicate:tagPredicate];

        tagsToDisplay = (NSMutableArray *)[self.note.managedObjectContext executeFetchRequest:hashtagRequest error:nil];
        [tagListTable reloadData];

        //If there are no matching hashtags, then let's hide the tag table.
        if ([tagsToDisplay count] == 0) 
        {
            [self hideTagTable];
            return;
        }

    }

The StringChecker class is a custom one that I wrote, it just has class methods that parse the strings. I made StringChecker a class because the methods are used in several places in the app. Here's the method:

    #pragma mark - Hashtag Methods
+(NSArray *)rangesOfHashtagsInString:(NSString *)string {
    NSRegularExpression *hashtagDetector = [[NSRegularExpression alloc] initWithPattern:@"#\\w\\w*" 
                                                                                options:NSRegularExpressionCaseInsensitive 
                                                                                  error:nil];    
    NSArray *hashtagRanges = [hashtagDetector matchesInString:string
                                                      options:NSMatchingWithoutAnchoringBounds
                                                        range:NSMakeRange(0, string.length)];
    return hashtagRanges;
}

+(NSUInteger)numberOfHashtagsInString:(NSString *)string {
    NSRegularExpression *hashtagDetector = [[NSRegularExpression alloc] initWithPattern:@"#\\w\\w*" 
                                                                                options:NSRegularExpressionCaseInsensitive 
                                                                                  error:nil];
    NSUInteger numberOfHashtags = [hashtagDetector numberOfMatchesInString:string
                                                                   options:NSRegularExpressionCaseInsensitive
                                                                     range:NSMakeRange(0, string.length)];
    return numberOfHashtags;
}
ぶ宁プ宁ぶ 2024-12-25 02:26:41

我想出的另一种方法如下。

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 函数中,我为正在键入的 # 放置了一个监听器,它开始记录下面的字符散列,直到用户键入一个空格,此时它会重置。

if ([text isEqualToString:@"#"]) {
    recordingHashTag = YES;
    startParse = range.location;

}else if ([text isEqualToString:@" "]) {
    currentHashTag = nil;
    recordingHashTag = NO;
    theTable.hidden = YES;

}

if (recordingHashTag == YES) {
    NSString *value;
    if (startParse > [textView.text length] - startParse) {
        value = [textView.text substringWithRange:NSMakeRange(startParse, [textView.text length] - startParse)];
        [self filterHashTagTableWithHash:value];
    }
}

如果 BOOL reportingHashTag 设置为 YES,我会将包含主题标签文本的 substring 传递给一个搜索预先填充的主题标签数组的函数。如果存在匹配项,则会将该条目添加到经过过滤的主题标签数组中,并使用该数组动态填充 tableview

-(void)filterHashTagTableWithHash:(NSString *)hash{

    [self.filterHashTagArray removeAllObjects];

    for (NSString *hashTag in self.hashTagArray ){
        NSRange result = [hashTag rangeOfString:hash options:NSCaseInsensitiveSearch];
        if (result.location != NSNotFound) {
            [filterHashTagArray addObject:hashTag];
        }
    }
    if (filterHashTagArray.count) {
        theTable.hidden = NO;
    }else{
        theTable.hidden = YES;
    }

    [self.theTable reloadData];
}

最后一步是当用户单击表中的条目时插入哈希标签。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = (UITableViewCell*)[self tableView:theTable cellForRowAtIndexPath:indexPath];

    NSString *newString = [textViewComment.text stringByReplacingCharactersInRange:NSMakeRange(startParse, [textViewComment.text length] - startParse) withString:cell.textLabel.text];
    textViewComment.text = newString;
}

只是不要忘记在用户退格中间哈希标记时清除变量。

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 ([text isEqualToString:@"#"]) {
    recordingHashTag = YES;
    startParse = range.location;

}else if ([text isEqualToString:@" "]) {
    currentHashTag = nil;
    recordingHashTag = NO;
    theTable.hidden = YES;

}

if (recordingHashTag == YES) {
    NSString *value;
    if (startParse > [textView.text length] - startParse) {
        value = [textView.text substringWithRange:NSMakeRange(startParse, [textView.text length] - startParse)];
        [self filterHashTagTableWithHash:value];
    }
}

If the BOOL recordingHashTag is set to YES I pass the substring 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 the tableview on the fly.

-(void)filterHashTagTableWithHash:(NSString *)hash{

    [self.filterHashTagArray removeAllObjects];

    for (NSString *hashTag in self.hashTagArray ){
        NSRange result = [hashTag rangeOfString:hash options:NSCaseInsensitiveSearch];
        if (result.location != NSNotFound) {
            [filterHashTagArray addObject:hashTag];
        }
    }
    if (filterHashTagArray.count) {
        theTable.hidden = NO;
    }else{
        theTable.hidden = YES;
    }

    [self.theTable reloadData];
}

The final step is to insert the hash tag when the user clicks on the entry in the table.

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = (UITableViewCell*)[self tableView:theTable cellForRowAtIndexPath:indexPath];

    NSString *newString = [textViewComment.text stringByReplacingCharactersInRange:NSMakeRange(startParse, [textViewComment.text length] - startParse) withString:cell.textLabel.text];
    textViewComment.text = newString;
}

Just don't forget to clear out your variables when a user backspaces mid hash tag.

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