You've probably meant to use
self.explainationTV.attributedText = attrString;
instead of
[[self.explainationTV textStorage] setAttributedString:attrString];
BTW it's explanation.
Frage
I'm trying to color some words in a UITextView in an iPhone app but I'm getting an error and I'm starting to think that maybe it is not possible... I'm using this code:
-(void)colorText
{
NSString *word = @"duck";
NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString: explainationTV.text];
NSUInteger count = 0;
NSUInteger length = [attrString length];
NSRange range = NSMakeRange(0, length);
while(range.location != NSNotFound)
{
range = [[attrString string] rangeOfString:word options:0 range:range];
if(range.location != NSNotFound) {
[attrString addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(range.location, [word length])];
[attrString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(range.location, [word length])];
range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
count++;
}
}
[[self.explainationTV textStorage] setAttributedString:attrString];
}
and I'm getting this error:
-[UITextView textStorage]: unrecognized selector sent to instance 0x1f26ca00 2013-10-16 20:59:41.711 My App[19795:913] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITextView textStorage]: unrecognized selector sent to instance 0x1f26ca00'
I'm using the same code in a Mac app and it works fine... anyone knows if I am doing something wrong or it simply can not work in iPhone app? Thanks for any help... Massy
Lösung
You've probably meant to use
self.explainationTV.attributedText = attrString;
instead of
[[self.explainationTV textStorage] setAttributedString:attrString];
BTW it's explanation.