This question already has an answer here:
How to search(Predicate) content from list like Xcode suggestion?
(1 answer)
Closed 4 years ago.
How to highlight UILabel with formatted phone number while searching?
Example:
A cell is displaying a list of contacts with a number like: +381 60 0000000
If I type 3, I want + and 3 to be highlighted (colored differently)
If I type 38160, I want +381 60 to be highlighted (colored differently).
And so on...
The main point of this question is how to color the matching numbers by jumping over white spaces and + sign?
Here is what I've tried so far, but it works only if I remove white spaces and + sign from the label's text value:
// Highlighted search for numbers
NSString *phoneString = #"+381 60 0000000";
NSString *searchString = #"38160";
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:phoneString];
NSRange selectedRange = [[string string] rangeOfString:searchString options:NSRegularExpressionSearch];
if (selectedRange.location != NSNotFound) {
[string beginEditing];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:selectedRange];
[string endEditing];
}
cell.phoneNumberLabel.attributedText = string;
Thanks to #Larme, I have answered my own question :)
// Highlighted search for numbers
NSString *phoneString = #"+381 60 000343";
NSString *firstComponent = #"\\+?"; // Skip a leading + char
NSMutableArray *digits = [[NSMutableArray alloc] init];
[self.searchString enumerateSubstringsInRange:NSMakeRange(0, [self.searchString length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
[digits addObject:substring];
}];
NSString *pattern = [[NSString alloc] initWithFormat:#"%#%#", firstComponent, [digits componentsJoinedByString:#"\\s?"]];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:phoneString];
NSRange selectedRange = [[string string] rangeOfString:pattern options:NSRegularExpressionSearch];
if (selectedRange.location != NSNotFound) {
[string beginEditing];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:selectedRange];
[string endEditing];
}
cell.phoneNumberLabel.attributedText = string;
Related
I have an task where I need to count the occurrences of errors in a log file and I know how to do that. Now Im trying to change the font color of these occurrences. I have it kinda working but it doesn't change the whole word to the wanted color and for the next occurrence of that string it shifts over 3 characters.
See image below.
I searched for the word "Checked" and it gave me these results.
Below is the code that I am using
NSArray * lines = [words componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
wordresult = [lines componentsJoinedByString:#""];
if (occS2 == 1)
{
NSString * box2 = [_occSearchTwoTextBox stringValue];
NSUInteger countFatal = 0, length4 = [wordresult length];
NSRange range4 = NSMakeRange(0, length4);
while(range4.location != NSNotFound)
{
range4 = [wordresult rangeOfString: box2 options:NSCaseInsensitiveSearch range:range4];
[self.stringLogTextView setTextColor:[NSColor redColor] range:range4];
NSLog(#"Occurance Edited");
if(range4.location != NSNotFound)
{
range4 = NSMakeRange(range4.location + range4.length, length4 - (range4.location + range4.length));
countFatal++;
}
}
NSString * FatalCount = [NSString stringWithFormat:#"%lu", (unsigned long)countFatal];
[_customSearchTwoTextBox setStringValue:FatalCount];
}
Can anyone please point me to where to why its shifting? I can only assume that it has something to do with my range but I'm not sure what to do to resolve.
Thanks for everyones time!
I'm not sure why your method isn't working correctly, but I would do it a different way. Using enumerateSubstringsInRange:options:usingBlock:, you can enumerate your string by word, and get the range of each word passed in to the method for you. If the word is "Checked", you can increment your count and also set the attributes for that range of a mutable attributed string. Here is an example of how to use that method,
NSString *theText = #"] Tables initialized\n]Database version Checked\n]Got login id-1\n] Tables initialized\n]Database version Checked\n]Got login id-1\n] Tables initialized\n]Database version Checked\n]Got login id-1\n";
NSMutableAttributedString *attrib = [[NSMutableAttributedString alloc] initWithString:theText];
NSDictionary *dict = #{NSForegroundColorAttributeName:[NSColor greenColor]};
__block int count = 0;
[theText enumerateSubstringsInRange:NSMakeRange(0, theText.length) options:NSStringEnumerationByWords usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
if ([substring isEqualToString:#"Checked"]) {
[attrib setAttributes:dict range:substringRange];
count ++;
};
}];
self.textView.textStorage.attributedString = attrib;
NSLog(#"count is: %d",count);
I have the following string
22\nShaʻban\n1435
and i'm using NSMutableAttributedString to format the above string using multiple fonts as follows:
NSString* orgString=#"22\nShaʻban\n1435";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:[dateStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
UIFont *dayFont=[UIFont fontWithName:#"Helvetica-Bold" size:40.0f];
UIFont *monthFont=[UIFont fontWithName:#"Arial" size:22.0f];
UIFont *yearFont=[UIFont fontWithName:#"Arial" size:20.0f];
//format day part
[attString addAttribute:NSFontAttributeName value:dayFont range:NSMakeRange(0,2)];
//format month part
[attString addAttribute:NSFontAttributeName value:monthFont range:NSMakeRange(3,[self indexOf:[dateStr substringFromIndex:3] andSearchChar:#"\n"])];
//format year part, app crashes here
[attString addAttribute:NSFontAttributeName value:yearFont range:NSMakeRange([self indexOf:[dateStr substringFromIndex:3] andSearchChar:#"\n"]+1,[dateStr length])];
- (int) indexOf:(NSString*)orgStr andSearchChar:(NSString *)charToSearc {
NSRange range = [orgStr rangeOfString:charToSearc];
if ( range.length > 0 ) {
return range.location;
} else {
return -1;
}
}
i don't know why it crashes when trying to format the last part, i made arrange from the last position in part two +1 to the length of the string, any help please
NSRange NSMakeRange (
NSUInteger loc,
NSUInteger len
);
A range is a location and a length, not a start and end location. So you need to change how you calculate the range content.
Or, split the source string apart, create an attributed string for each part and then append them together.
I'd suggest this:
NSString* orgString=#"22\nShaʻban\n1435";
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] init];
UIFont *dayFont = [UIFont fontWithName:#"Helvetica-Bold" size:40.0f];
UIFont *monthFont = [UIFont fontWithName:#"Arial" size:22.0f];
UIFont *yearFont = [UIFont fontWithName:#"Arial" size:20.0f];
NSArray *array = [orgString componentsSeparatedByString:#"\n"];
[attrString appendAttributedString:[[NSAttributedString alloc] initWithString:[array objectAtIndex:0] attributes:#{NSFontAttributeName: dayFont}]];
[attrString appendAttributedString:[[NSAttributedString alloc] initWithString:#"\n"]];
[attrString appendAttributedString:[[NSAttributedString alloc] initWithString:[array objectAtIndex:1] attributes:#{NSFontAttributeName: monthFont}]];
[attrString appendAttributedString:[[NSAttributedString alloc] initWithString:#"\n"]];
[attrString appendAttributedString:[[NSAttributedString alloc] initWithString:[array objectAtIndex:2] attributes:#{NSFontAttributeName: yearFont}]];
So, no NSRange issue. Plus as said by #Wain, you misunderstood what's a NSRange.
Instead of what you're doing, once you found the location, you had to put, as second parameter of NSMakeRange: nextLocation-currentLocation. Id est, for last one, something like this:
NSMakeRange([self indexOf:[dateStr substringFromIndex:3] andSearchChar:#"\n"]+1,
[dateStr length]-[self indexOf:[dateStr substringFromIndex:3] andSearchChar:#"\n"]+1)
NSString * strTimeBefore = [timeBefore componentsJoinedByString:#" "];
NSString * strTimeAfter = [timeAfter componentsJoinedByString:#" "];
I want the resulting string to be an NSAttributedString where the time in strTimeAfter is in bold
You probably want something like:
NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName];
NSString *yourString = [NSString stringWithFormat:#"%# %#", strTimeBefore, strTimeAfter;
// start at the end of strTimeBefore and go the length of strTimeAfter
NSRange boldedRange = NSMakeRange([strTimeBefore length] + 1, [strTimeAfter length]);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];
[attrString beginEditing];
[attrString addAttribute:NSFontAttributeName
value:boldFontName
range:boldedRange];
[attrString endEditing];
And my answer is cribbed from Jacob's answer to this very closely related question.
Take two attribute string in that store your first string into one attribute string without changing its attributes, In second attribute string store your second string with changing its attibutes and then append both attribute string into one NSMutableAttributeString Try like this below:-
NSString * strTimeBefore = [timeBefore componentsJoinedByString:#" "];
NSString * strTimeAfter = [timeAfter componentsJoinedByString:#" "];
NSAttributedString *attrBeforeStr=[[NSAttributedString alloc]initWithString:strTimeBefore];
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
[attributes setObject:[NSColor yellowColor] forKey:NSBackgroundColorAttributeName];
NSFont *font = [[NSFontManager sharedFontManager] fontWithFamily:#"Arial" traits:NSBoldFontMask weight:5 size:14];
[attributes setObject:font forKey:NSFontAttributeName];
NSAttributedString *attrAftStr=[[NSAttributedString alloc]initWithString:strTimeAfter attributes:];
NSMutableAttributedString *string=[[NSMutableAttributedString alloc] init];
[string appendAttributedString:attrBeforeStr];
[string appendAttributedString:strTimeAfter];
Note: You can change font color as well in attribute string, if it is required.
I need to change the background color of some particular words in a text that stay in a textView. Something similar to what happens in Firefox when you seearch for a word... So lets say I have a textView with this text
"A man is sitting in front of my porch and another man is calling him"
and I want to change background color to the 2 occurence of the word
"man"
... how could I do that?
I know that there is NSAttributedString to do this kind of things but I can not understand how to modify only some particular words... in the examples I found Googling it there were only examples of how to change the first 5 characters or things like this...
Try this:
NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString: #"Your String"];
NSUInteger count = 0;
NSUInteger length = [attrString length];
NSRange range = NSMakeRange(0, length);
while(range.location != NSNotFound)
{
range = [[attrString string] rangeOfString:#"YOURWORD" options:0 range:range];
if(range.location != NSNotFound) {
[attrString addAttribute:NSBackgroundColorAttributeName value:YOURCOLOR range:NSMakeRange(range.location, [word length])];
range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
count++;
}
}
Given a string, I need to obtain a count of each word that appears in that string. To do so, I extracted the string into an array, by word, and searched that way, but I have the feeling that searching the string directly is more optimal. Below is the code that I originally wrote to solve the problem. I'm up for suggestions on better solutions though.
NSMutableDictionary *sets = [[NSMutableDictionary alloc] init];
NSString *paragraph = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"text" ofType:#"txt"] encoding:NSUTF8StringEncoding error:NULL];
NSMutableArray *words = [[[paragraph lowercaseString] componentsSeparatedByString:#" "] mutableCopy];
while (words.count) {
NSMutableIndexSet *indexSet = [[NSMutableIndexSet alloc] init];
NSString *search = [words objectAtIndex:0];
for (unsigned i = 0; i < words.count; i++) {
if ([[words objectAtIndex:i] isEqualToString:search]) {
[indexSet addIndex:i];
}
}
[sets setObject:[NSNumber numberWithInt:indexSet.count] forKey:search];
[words removeObjectsAtIndexes:indexSet];
}
NSLog(#"%#", sets);
Example:
Starting string:
"This is a test. This is only a test."
Results:
"This" - 2
"is" - 2
"a" - 2
"test" - 2
"only" - 1
This is exactly what an NSCountedSet is for.
You need to break the string apart into words (which iOS is nice enough to give us a function for so that we don't have to worry about punctuation) and just add each of them to the counted set, which keeps track of the number of times each object appears in the set:
NSString *string = #"This is a test. This is only a test.";
NSCountedSet *countedSet = [NSCountedSet new];
[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
options:NSStringEnumerationByWords | NSStringEnumerationLocalized
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){
// This block is called once for each word in the string.
[countedSet addObject:substring];
// If you want to ignore case, so that "this" and "This"
// are counted the same, use this line instead to convert
// each word to lowercase first:
// [countedSet addObject:[substring lowercaseString]];
}];
NSLog(#"%#", countedSet);
// Results: 2012-11-13 14:01:10.567 Testing App[35767:fb03]
// <NSCountedSet: 0x885df70> (a [2], only [1], test [2], This [2], is [2])
If I had to guess, I would say NSRegularExpression for that. Like this:
NSUInteger numberOfMatches = [regex numberOfMatchesInString:string
options:0
range:NSMakeRange(0, [string length])];
That snippet was taken from here.
Edit 1.0:
Based on what Sir Till said:
NSString *string = #"This is a test, so it is a test";
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
NSArray *arrayOfWords = [string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
for (NSString *word in arrayOfWords)
{
if ([dictionary objectForKey:word])
{
NSNumber *numberOfOccurences = [dictionary objectForKey:word];
NSNumber *increment = [NSNumber numberWithInt:(1 + [numberOfOccurences intValue])];
[dictionary setValue:increment forKey:word];
}
else
{
[dictionary setValue:[NSNumber numberWithInt:1] forKey:word];
}
}
You should be careful with:
Punctuation signs. (near other words)
UpperCase words vs lowerCase words.
I think that's really bad idea that you trying to search a words among the long paragraph with a loop. You should use a regular expression to do that! I know it's not easy at first time to learn it but it's really worth to know it! Take look at this case Use regular expression to find/replace substring in NSString