iPhone簡単文字検索

文章から文字を検索してマーカーを表示するiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic, weak) UITextView *textView;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:0.5];

    self.textView = [self createTextView:[self createWords]];

    self.textView.userInteractionEnabled = NO;

    

    [self createButton];

}

– (NSString *)createWords

{

    NSArray *seeds = @[@”book”, @”banana”, @”,”, @”.”, @”car”, @”is”, @”this”, @”that”, @”1″, @”A”];

    NSMutableString *words = [NSMutableString stringWithString:@””];

    for (int i=0; i<200; i++) {

        [words appendString:seeds[arc4random() % seeds.count]];

        [words appendString:@” “];

    }

    

    return words;

}

– (UITextView *)createTextView:(NSString *)words

{

    NSAttributedString *atts =  [[NSAttributedString alloc] initWithString:words attributes:@{NSFontAttributeName: [UIFont fontWithName:@”Chalkduster” size:12]}];

    

    CGRect frame = CGRectInset(self.view.bounds, 30, 30);

    

    NSTextStorage *strage = [[NSTextStorage alloc] initWithAttributedString:atts];

    NSLayoutManager *layout = [[NSLayoutManager alloc] init];

    

    NSTextContainer *container = [[NSTextContainer alloc] initWithSize:frame.size];

    [layout addTextContainer:container];

    [strage addLayoutManager:layout];

    

    

    UITextView *tv = [[UITextView alloc] initWithFrame:frame textContainer:container];

    tv.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];

    [self.view addSubview:tv];

    

    return tv;

}

– (void)searchWord:(NSString *)search source:(NSString *)source index:(int)index

{

    NSRange range = NSMakeRange(index, search.length);

    NSTextContainer *container = self.textView.textContainer;

    CGRect rect = [container.layoutManager boundingRectForGlyphRange:range inTextContainer:container];

    rect = CGRectMake(rect.origin.x, rect.origin.y + 10, rect.size.width, rect.size.height);

    

    NSString *str = [source substringWithRange:range];

    

    UIView *marker = [[UIView alloc] initWithFrame:rect];

    marker.backgroundColor = [UIColor yellowColor];

    marker.alpha = 0.5;

    [self.textView addSubview:marker];

    [UIView animateWithDuration:0.02 animations:^{

        marker.alpha = 0.2;

    } completion:^(BOOL finished) {

        

        if ([str isEqual:search]) {

            marker.alpha = 0.4;

        } else {

            [marker removeFromSuperview];

        }

        if (index < source.length – search.length) {

            [self searchWord:search source:source index:index + 1];

        }

    }];

}

– (void)createButton

{

    UILabel *btn = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 160, 160)];

    btn.center = CGPointMake(160, CGRectGetMaxY(self.view.frame));

    btn.backgroundColor = [UIColor blackColor];

    btn.text = @”Search banana!\n\n\n\n”;

    btn.numberOfLines = 0;

    btn.textAlignment = NSTextAlignmentCenter;

    btn.textColor = [UIColor whiteColor];

    btn.font = [UIFont boldSystemFontOfSize:15];

    btn.layer.cornerRadius = 80;

    btn.layer.masksToBounds = YES;

    [self.view addSubview:btn];

    

    btn.userInteractionEnabled = YES;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(startSearch:)];

    [btn addGestureRecognizer:tap];

}

– (void)startSearch:(UIGestureRecognizer *)tap

{

    NSString *searchWord = @”banana”;

    NSString *words = self.textView.attributedText.string;

    [self searchWord:searchWord source:words index:0];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end