ios NSAttributedString--从文章中标记高亮

来源:互联网 发布:linux rm -rf 命令撤回 编辑:程序博客网 时间:2024/05/22 12:33

//小鑫原创转载请注明出处http://blog.csdn.net/wodeni521877

纯新手完全是手打得 不会用代码文本那个,所以大家复制粘贴的时候一定要小心点,注意大小写可能会有错字

上一个IOS文章小鑫说要再写一篇关于NSAttributedString的文章,这篇文章要写的是从一篇文章中为你想要标记个文字进行

高亮标记,废话不多说直接上代码。

NSMutableAttibutedString *seachAttributedString =[ [NSMutableAttributedString alloc] initWithString:@"111"];

NSString *beSearchedString = [NSString stringWithFormat:@"1112222111"];

NSRange seachRage = NSmakeRange (0,[seachAttributedString length]);

do{

NSRange rangeOfSearch = [seachAttributedString.mutableString rangeOfString:beSearchedString

                                                                                                                   options:NSCaseInsensitiveSearch

                                                                                                                     range:seachRage ];

if(rangeOfSearch.location != NSNotFound)

{

  seachRage.location = rangeOfSearch.location + rangeOfSearch.length;

  seachRage.length = seachAttributedString.length - seachRage.location;

  NSRange rangeMake = NSMakeRange (rangeOfSearch.location,rangeOfSearch.length);

  [seachAttributedString addAttribute:NSForegroundColorAttributedName

                                                 value: [UIColor redColor]

                                                  range:rangeMake];

}

}while(1)

 

原创粉丝点击