1

我正在尝试在 iPhone 应用程序的 UITextView 中为某些单词着色,但出现错误,我开始认为这可能是不可能的......我正在使用这段代码:

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

我收到了这个错误:

-[UITextView textStorage]:无法识别的选择器发送到实例 0x1f26ca00 2013-10-16 20:59:41.711 My App[19795:913] *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[UITextView textStorage]:无法识别选择器发送到实例 0x1f26ca00'

我在 Mac 应用程序中使用相同的代码,它工作正常......有人知道我做错了什么还是它根本无法在 iPhone 应用程序中工作?感谢您的帮助...马西

4

1 回答 1

0

你可能打算使用

self.explainationTV.attributedText = attrString;

代替

[[self.explainationTV textStorage] setAttributedString:attrString];

顺便说一句,这是解释

于 2013-10-16T19:09:27.413 回答