English 中文(简体)
NIAT属性标签设置文本颜色不工作
原标题:NIAttributedLabel settextcolor not working

我使用NIAttriatedLabel 在文本上显示链接 。

            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]];  

但最后一行工作不正确, 尽管 < 强势 > stringPh < / 强性 > 位于 < 强性 > 编辑Text < /强性 > 中。 所有文本都在蓝色中出现 。

最佳回答

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

您是否绝对确定您的字符串 stringPh >strection 的实际子字符串 EditedText ?

问题回答

这里是我的工作守则

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];




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签