6

TwitterやTweetbotなどのハッシュタグを使用するアプリを作成します。メッセージを入力するときにハッシュタグ記号を入力する場合は、現在入力しているタグと一致するタグを提案したいと思います。

UITableViewを表示してハッシュタグのリストを表示する方法はすでに理解していますが、理解できないのは次の方法です。

  1. NSRange入力されている現在の単語を取得し、
  2. その範囲がハッシュタグ(NSRegularExpression @"#\\w\\w*")のようにフォーマットされているかどうかを確認します
  3. (これからは、一致するハッシュタグを検索してUITableViewに表示するためのコードを理解しました)

誰かがステップ1と2で私を助けることができますか?を使用することを考えていましたがtextViewDidChange:、文字が変わるたびにメソッドを実行し続けると、アプリのパフォーマンスが低下するのではないかと心配しています。

ありがとう!

4

2 に答える 2

8

私はそれを考え出した!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;
}
于 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 に答える