NIAttributedLabel settextcolor not working

I use NIAttributedLabel to display links to text.

  NIAttributedLabel *label; label = [[NIAttributedLabel alloc] initWithFrame:rect]; label.delegate = self; label.font = [UIFont fontWithName:@"Helvetica" size:MAIN_FONT_SIZE]; label.textAlignment = UITextAlignmentLeft; label.lineBreakMode = UILineBreakModeWordWrap; label.numberOfLines = 0; label.backgroundColor = [UIColor clearColor]; label.highlightedTextColor = [UIColor whiteColor]; label.text = strEditedText; label.textColor = [UIColor blackColor]; [label setTextColor:[UIColor blueColor] range:[strEditedText rangeOfString:stringPh]]; 

But the last line does not work correctly, although stringPh is in strEditedText . All text is in blue.

+4
source share
2 answers

I installed an example that you can download here , and I found that it works great.
FROM:

 NSString * strEditedText= @"I'm text"; NSString *stringPh = @"I'm"; 

The simulator correctly selects the right part of the text: enter image description here

Are you absolutely sure that your stringPh string is the actual substring strEditedText ?

+3
source

Here is the working code for me

 NSString * strEditedText= @"Please contact abc at 800.493.0016, option #3 for further assistance."; NSString *stringPh = @"800.493.0016"; NIAttributedLabel *label; label = [[NIAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 320, 100)]; label.delegate = self; label.font = [UIFont fontWithName:@"Helvetica" size:MAIN_FONT_SIZE]; label.textAlignment = UITextAlignmentLeft; label.lineBreakMode = UILineBreakModeWordWrap; label.numberOfLines = 0; label.backgroundColor = [UIColor clearColor]; label.highlightedTextColor = [UIColor whiteColor]; label.text = strEditedText; label.textColor = [UIColor blackColor]; [label setTextColor:[UIColor blueColor] range:[strEditedText rangeOfString:stringPh]]; [self.view addSubview:label]; 
+2
source

Source: https://habr.com/ru/post/1414121/


All Articles