首页 > 代码库 > iOS 将一串字符里面的某个字符全部标志出来
iOS 将一串字符里面的某个字符全部标志出来
NSMutableString * mutStr = [NSMutableString stringWithString:@"aaabbbbaaaccc"]; NSString * str = @"a"; NSRange range = {0,mutStr.length}; [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range]; [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range]; NSMutableAttributedString * attr = [[NSMutableAttributedString alloc]initWithString:mutStr]; NSError * error; NSRegularExpression * express = [NSRegularExpression regularExpressionWithPattern:str options:NSRegularExpressionCaseInsensitive error:&error]; NSArray<NSTextCheckingResult *> * result = [express matchesInString:mutStr options:0 range:range]; for (NSTextCheckingResult * match in result) { NSRange range = [match range]; [attr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range]; } UILabel * lbl = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 40)]; [self.view addSubview:lbl]; lbl.textColor = [UIColor blueColor]; lbl.attributedText = attr;
iOS 将一串字符里面的某个字符全部标志出来
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。