8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Objective-C テキストから文字列を検索し色をつける

Last updated at Posted at 2016-06-19

文章中の文字列を検索する手順

  1. (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask range:(NSRange)searchRange;を使いNSRangeを取得

2.NSRangeを配列に格納したい

3.NSRangeはオブジェクト型ではないのでNSValueでオブジェクト型に変換

-(NSArray*)searchWordWithRange:(NSString*)targetText word:(NSString*)
searchWord
{
    //! rangeオブジェクト(NSValue)の格納配列
    NSMutableArray* arrayRangeValue = [[NSMutableArray alloc] init];
    //! range初期化
    NSRange range = NSMakeRange(0, targetText.length);
    //! targetTextから検索 見つからなかったらwhile文を抜ける
    while ([targetText rangeOfString:searchWord options:NSLiteralSearch
                               range:range].location != NSNotFound) {
        //! 発見したrange
        NSRange rangeDetect = [targetText rangeOfString:searchWord
                                                options:NSLiteralSearch range:range];
        //! NSRangeをNSValueに変換
        [arrayRangeValue addObject:[NSValue valueWithRange:rangeDetect]];
        //! rangeのオフセット変更
        range = NSMakeRange(rangeDetect.location+rangeDetect.length,
                            targetText.length - (rangeDetect.location+rangeDetect.length));
    }
    return arrayRangeValue;
}

#NSRange(NSValue)を格納した配列とテキスト本文を引数にして色をつける関数

-(NSAttributedString*)highlightStringByRanges:(NSString*)targetText
                                       ranges:(NSArray*)arrayRange
{
    NSMutableAttributedString* stringAttributed = [[
                                                    NSMutableAttributedString alloc] initWithString:targetText
                                                   
                                                   attributes:nil];
    for (NSValue* rangeObject in arrayRange) {
        NSRange range = [rangeObject rangeValue];
        [stringAttributed addAttribute:NSBackgroundColorAttributeName 
                                 value:[UIColor cyanColor] range:range];
    }
    return stringAttributed;
}

#ViewDidLoadで上記二つのメソッドを呼び出す

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSBundle* bundle = [NSBundle mainBundle];
    //! 青空文庫の「こころ」を取得
    NSString* stringPath = [bundle pathForResource:@"kokoro" ofType:@"txt"];
    NSString* stringText = [NSString stringWithContentsOfFile:stringPath encoding:NSShiftJISStringEncoding error:nil];
    //! "私"を検索
    NSArray* arrayRange = [self searchWordWithRange:stringText word:@"私"];
    //! UITextViewのattributedTextに設定すること
    _textview.attributedText = [self highlightStringByRanges:stringText ranges:arrayRange];
    
}

#夏目漱石の「こころ」から"私"を検索した結果

スクリーンショット 2016-06-19 22.04.28.png
8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?