I have an issue with a NSAttributedString in a UITextField,
So what I want is that the usernames in the the textfield are in color blue. This code is working for this purpose but the problem is, when user hit back in the textfield until a username, the rest of the text become blue.
example:
(let's suppose the "|" character is the actual typing position)
hello this is Franck
, how are you?|
hello this is Franck
|
hello this is Franck, how are you?
|
Here is some of my code for reference.
int i = 0;
for (NSString * username in _totalUsername){
NSRange mentionHere = [editText rangeOfString:_totalMentionTyped[i]];
if(mentionHere.location != NSNotFound){
[attributedString replaceCharactersInRange:[editText rangeOfString:_totalMentionTyped[i]] withString:username];
}
NSRange range = [[attributedString string] rangeOfString:username];
while(range.location != NSNotFound){
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];
[attributedString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:15.0] range:range];
range = [[attributedString string] rangeOfString:username options:0 range:NSMakeRange(range.location + 1, [[attributedString string] length] - range.location - 1)];
}
i++;
}
self.commentTextField.attributedText = attributedString;