Reputation: 112
I need to have a UITextField
replace certain words with bolded words while the user is typing. In a method specified textViewDidChange
I have:
-(void)textViewDidChange:(UITextView *)textView {
self.notepadTextView.font = [UIFont fontWithName:@"ProximaNova-Regular" size:20];
NSString *textEntryContents = [[self notepadTextView ]text];
[gCore processSpeechText:textEntryContents]; //program specific function enabling speech
...
textEntryContents = [textEntryContents stringByReplacingOccurrencesOfString:@" performance" withString:@"\n\nPerformance"];
UIFont *fontBold = [UIFont fontWithName:@"ProximaNova-Bold" size:20];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:textEntryContents];
[string addAttribute:NSFontAttributeName value:fontBold range:NSMakeRange(0,textEntryContents.length)];
self.notepadTextView.attributedText = string;
}
This formats the text that the user inputs so that every time the word "performance" is typed, it will start a new line. This works well. I tried to make it so that every time "performance" is typed, it also bolds the word. The bolding aspect works; however it bolds everything, not just the word performance.
If user types:
"performance tested well, but needs more updating."
What should happen:
"Performance
tested well, but needs more updating."
What happens:
"Performance
tested well, but needs more updating."
The self.notepadTextView.attributedText = string
seems to override the previous UIFont
change for every word, and I do not know how to make a UITextField
accept two "self.textView.text 's" without one overriding the other. Also, I am at a loss of what I could possibly change the attributed string range to since the range would be changing constantly for any index that the word "performance" is typed.
Let me know if you have any questions! But please help!
Upvotes: 0
Views: 350
Reputation: 26026
Using a NSRegularExpression
(aka Regex), you could to that:
UIFont *normalFont = [UIFont fontWithName:@"HelveticaNeue" size:20];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"hey performance tested well, but needs more updating"
attributes:@{NSFontAttributeName: normalFont}];
NSError *regexError;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"Performance"
options:NSRegularExpressionCaseInsensitive error:®exError];
if (!regexError)
{
NSArray *allMatches = [regex matchesInString:[attributedString string]
options:0
range:NSMakeRange(0, [[attributedString string] length])];
UIFont *boldFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:20];
for (NSTextCheckingResult *aMatch in allMatches)
{
NSRange matchRange = [aMatch range];
[attributedString setAttributes:@{NSFontAttributeName:boldFont}
range:matchRange];
}
}
Note: I used Helvetica Neue
, and its bold version since I don't have your font.
That's an example I used to try, so you may replace the attributedString original text with textEntryContents
.
For more information about Regex, I liked this tutorial that seems clear about them. It's one of the first entry with Google searching about NSRegularExpression
. Their "cheat sheet" may be quite handy, if you need more complex thing (I didn't check if it need to have a space after the string searched, etc.), and change the pattern Performance
, with something like [NSString stringWithFormat:@"StartingRegexWithSpecialCharactersPerformanceEndingRegexWithSpecialCharacters]
in matchesInString:options:range:
.
Upvotes: 2