为iPhone UITextView打字(如Twitter)时建议#标签

时间:2011-11-26 19:46:14

标签: iphone ios regex uitextview nsregularexpression

我正在构建一个使用主题标签的应用,例如Twitter或Tweetbot。当您键入邮件时,如果您键入主题标签符号,我建议您使用与您当前键入的标签相匹配的标签。

我已经想出如何让UITableView出现并显示一个主题标签列表,但我无法弄清楚如何执行以下操作:

  1. 获取当前输入单词的NSRange
  2. 查看该范围是否格式化为主题标签(NSRegularExpression @"#\\w\\w*"
  3. (从现在开始,我已经找到代码来搜索匹配的hashtags并在UITableView中显示它们)
  4. 任何人都可以帮我完成第1步和第2步吗?我一直在考虑使用textViewDidChange:,但我担心如果每次角色更改时我都会不断运行方法,那么应用程序的性能可能会受到影响。

    谢谢!

2 个答案:

答案 0 :(得分:7)

我明白了!我使用textViewDidChange:textViewDidChangeSelection:方法结束了。

要获取当前主题标签的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;
}

答案 1 :(得分: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;
}

当用户退回中间哈希标记时,不要忘记清除变量。