2015년 2월 13일 금요일

문장에서 검색어와 일치하는 모든 단어 볼드처리 하기

{
    UIFont *boldFont = [UIFont boldSystemFontOfSize:10];
    UILabel *sentenceLabel = [[UILabel alloc] init];
    sentenceLabel.text = @"If you still feel the need to clean your ear, anything you can easily see or touch with the pad of your finger is fair game. But if you want to snake into your ear with a narrow tool, just remember this advice from Chandrasekhar: Nothing smaller than your elbow.";
    NSString *sentence = sentenceLabel.text;
    NSString *keyword = @"your";
    [sentence enumerateSubstringsInRange:NSMakeRange(0, [sentence length])
                                   options:NSStringEnumerationByWords
                                usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                                    if ([substring isEqualToString:keyword]){
                                        NSLog(@"%@", substring);
                                        [attributedText addAttribute:NSFontAttributeName value:boldFont range:substringRange];
                                    }
                                }];
        [sentenceLabel setAttributedText:attributedText];
    }

댓글 없음:

댓글 쓰기