4

I am using NIAttributedLabel for displaying links on 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 last line is not working correctly although stringPh is in strEditedText. All the text is coming in blue.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nitish
  • 13,845
  • 28
  • 135
  • 263
  • What are the values of stringPH and strEditedText? – Scott Forbes May 28 '12 at 06:03
  • **strEditedText** is : Please contact abc at 800.493.0016, option #3 for further assistance. And **stringPh** is 800.493.0016 – Nitish May 28 '12 at 06:23
  • Can you please `NSLog` each of your strings just before the last line? The only thing that may cause this (all text in blue) is when the two strings are the same (hence a range from start to end). I have copy-pasted your code using static strings with the values you provided and it works fine. – Alladinian May 28 '12 at 09:51

2 Answers2

3

I set up an example which you can download here, and I found that it worked perfectly.
With:

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

the simulator correctly highlights the right portion of the text: enter image description here

Are you absolutely sure your string stringPh is an actual substring of strEditedText?

CodaFi
  • 43,043
  • 8
  • 107
  • 153
2

Here is 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];
Maulik
  • 19,348
  • 14
  • 82
  • 137