我想从UITextView文本的特定单词中检测“_”
我已经试过了:
但这只计算第一个下划线的范围。
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:textView.text]; NSRange range=[textView.text rangeOfString:@"_"]; [string addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:range]; textView.attributedText=string;
我的输出就像这样
Tadeusz_Kościuszko是一名军事领袖,后来成为民族英雄 波兰。 Jules_Massenet 1910年歌剧首演巴黎的海报 Don_Quichotte。
我想要这样的事情:
TadeuszKościuszko是一名军事领袖,后来成为民族英雄 波兰。 Jules Massenet 1910年歌剧首演的巴黎海报 Don Quichotte。
另外,我想从名称中清除下划线的颜色。不是来自UITextView的其他文本
我不想从UITextView的整个文本中删除它,而只是从特定的单词中删除它
有人提出建议吗?
谢谢。
答案 0 :(得分:4)
textView.text = [textView.text stringByReplacingOccurrencesOfString:@"_" withString:@" "];
答案 1 :(得分:4)
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:textView.text];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(_)" options:kNilOptions error:nil];
NSRange range = NSMakeRange(0,string.length);
[regex enumerateMatchesInString:string options:kNilOptions range:range usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSRange subStringRange = [result rangeAtIndex:1];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:subStringRange];
}];
textView.attributedText=string;
答案 2 :(得分:1)
NSString *str = @"This_is_a_string";
str = [str stringByReplacingOccurrencesOfString:@"_"
withString:@" "];
// str = "This is a string"
更多关于NSString
答案 3 :(得分:1)
试试这个 -
NSString *str = @"Tadeusz_Kościuszko was a military leader who became a national hero in Poland. A poster for the Paris premiere of Jules_Massenet's 1910 opera Don_Quichotte.";
NSString *newStr = [str stringByReplacingOccurrencesOfString:@"_" withString:@" "];
NSLog(@"NEW STR ::::: %@",newStr);
答案 4 :(得分:0)
找到解决方法:)
NSString * strTemp = textView.text;
NSMutableAttributedString *mutString = [[NSMutableAttributedString alloc]initWithString:strTemp]; NSString *substring = @"_"; NSRange searchRange = NSMakeRange(0,mutString.length); NSRange wholeTextRange = [strTemp rangeOfString:strTemp]; [mutString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:wholeTextRange]; NSRange foundRange; while (searchRange.location < mutString.length) { searchRange.length = mutString.length-searchRange.location; foundRange = [strTemp rangeOfString:substring options:nil range:searchRange]; if (foundRange.location != NSNotFound) { // found an occurrence of the substring! do stuff here [mutString addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:foundRange]; searchRange.location = foundRange.location+foundRange.length; } else { // no more substring to find [txtComment setAttributedText:mutString]; break; } }
答案 5 :(得分:0)
如果您想有选择地删除下划线,您需要知道要从中移除它们的文本范围。
然后,您可以使用正则表达式来获取与该已知范围中的下划线匹配的范围数组。
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"_" options:0 error:&error];
NSArray *matchingRangesInSelectedRange = [regex matchesInString:yourString options:0 range:knownRangeToCheckForMatches];
然后,您可以迭代数组中的范围并执行您希望的任何更改。
答案 6 :(得分:0)
如果您知道哪些字符串不应该被清除或哪些字符串不被清除,您可以这样做:
NSString *stringWithUnderScore = @"A rather long text with some_odd_underscores but also names like André_Franquin & Albert_Uderzo";
NSSet *stringsThatShouldKeepUnderscore = [NSSet setWithObjects:@"some_odd_underscores", nil];
NSArray *singleStrings = [stringWithUnderScore componentsSeparatedByString:@" "];
NSMutableString *newCompleteString = [[NSMutableString alloc] init];
for (NSString *singleString in singleStrings) {
if (![stringsThatShouldKeepUnderscore containsObject:singleString]){
NSString *fixedString = [singleString stringByReplacingOccurrencesOfString:@"_" withString:@" "];
[newCompleteString appendString: fixedString];
} else{
[newCompleteString appendString:singleString];
}
[newCompleteString appendString:@" "];
}
NSLog(@"new string:%@",newCompleteString);
编辑:如果您不拥有此信息,您可以尝试使用某种解析器检查由“_”分隔的每个字符串是否都有大写字母,表示名称。不是故障安全的,但如果没有关于文本的知识,我真的看不到更好的选择。