문제

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.

도움이 되었습니까?

해결책

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?

다른 팁

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];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top