1
1

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.

NSAttributedStringを検索して特定文字列に色を付ける方法

Posted at
-(void) attributeStrings{
    NSString* plainStr = [zNSAttributedStringObj string];
    NSString* searchWord = @"\n    *";
    NSRange range = NSMakeRange(0,[plainStr length]);
    NSRange target;

    while ( target.location != NSNotFound ) {
        target = [plainStr rangeOfString:searchWord options:NSLiteralSearch range:range];
        NSDictionary *stringAttributes3 = @{ NSForegroundColorAttributeName : [NSColor blueColor]};
   
        if (target.location != NSNotFound) {
            [zNSAttributedStringObj setAttributes:stringAttributes3 range:target];
            NSInteger from = range.location + [searchWord length];
            NSInteger end = [plainStr length] - from;
            range = NSMakeRange(from, end);
        }
    }

}
//searchWordをNsRegularExpressionとNSDictionaryのNSArrayにでもすれば、まとめて色付けできるかな?
//いまのところ実行速度は期待していない。。。
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?