6

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

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

  1. 获取NSRange当前输入的单词,
  2. 查看该范围的格式是否类似于主题标签 ( NSRegularExpression @"#\\w\\w*")
  3. (从这里开始,我已经找到了搜索匹配主题标签并将它们显示在 UITableView 中的代码)

任何人都可以帮助我完成第 1 步和第 2 步吗?我一直在考虑使用textViewDidChange:,但我担心如果每次角色更改时我都不断地运行方法,应用程序的性能可能会受到影响。

谢谢!

4

2 回答 2

8

我想到了!我最终使用了textViewDidChange:andtextViewDidChangeSelection:方法。

为了获取NSRange当前正在输入的主题标签,我在文本字符串中的匹配项上运行了一个for循环。NSRegularExpression从那里,我曾经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;
}
于 2011-12-03T21:51:58.597 回答
4

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

- (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 recordingHashTag设置为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;
}

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

于 2013-06-07T17:21:06.083 回答