我发现这个小片段允许用户在NSTextView中创建文本链接:
-(void)setHyperlinkWithTextView:(NSTextView*)inTextView
{
    // create the attributed string
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
    // create the url and use it for our attributed string
    NSURL* url = [NSURL URLWithString: @"http://www.apple.com"];
    [string appendAttributedString:[NSAttributedString hyperlinkFromString:@"Apple Computer" withURL:url]];
    // apply it to the NSTextView's text storage
    [[inTextView textStorage] setAttributedString: string];
}
是否可以将链接指向我的应用程序中的某些资源,例如,能够解释链接并分派到相应视图/控制器的特定处理程序类?
好的,我们在这里:
NSTextViewNSMutableAttributedString满足通过使用Rob的代码(将自定义属性保存在NSAttributedString中),我已经取得了一些进展(我正在设法将数据写入磁盘),但是我无法恢复它(= NSKeyedUnarchiverreturn nil)。
编码方式:
// where MAS --> NSMutableAttributedString
NSData* stringData = [NSKeyedArchiver archivedDataWithRootObject:MAS];
解码:
NSMutableAttributedString* mas = (NSMutableAttributedString*)[NSKeyedUnarchiver unarchiveObjectWithData:dat];
有任何想法吗?任何可能的解决方法(即使不能使用NSCoder,我都怀疑它可以与RTF一起使用...)!
在我的应用程序中,我试图存档一个NSAttributedString对象,所以我NSData使用以下代码成功将其转换为对象:
NSData *attrStrData = [attrStr dataFromRange:NSMakeRange(0, attrStr.length) documentAttributes:NULL error:nil];
现在我想将此NSData对象转换回来NSAttributedString.我相信OSX你有一些方法,但有没有iOS?我找到了NSAttributedString+Encoding,但似乎对它的支持较少,而且没有CocoaPods.欢迎任何建议.
基于对此问题的接受答案,我编写了以下代码:
NSData* somedata;
somedata=[NSKeyedArchiver archivedDataWithRootObject:ts];
其中ts是一个NSAttributedString,它填充了一些文本和一些属性(在本例中为颜色).
当我执行此代码时,我收到此错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x6eb5b90'
我是NSCoder竞技场的新手,但上述问题的答案使我觉得这就是我所要做的.是吗?我错过了什么?
编辑:
在这种情况下,无法识别的选择器被发送到NSAttributedString中的颜色属性.当我像这样初始化字符串时:
NSAttributedString *ts = [[NSAttributedString alloc] initWithString:text attributes:self.currentAttributeDictionary];
字典就像这样构建:
self.currentAttributeDictionary=[NSDictionary dictionaryWithObjectsAndKeys:
                                 [self.currentColor CGColor],(NSString*)kCTForegroundColorAttributeName,
                                 nil];
字典的NSLog产生了这个:
New dictionary is: {
CTForegroundColor = "<CGColor 0x6eb5b90> [<CGColorSpace 0x6e968c0> (kCGColorSpaceDeviceRGB)] ( 1 1 0 1 )";}
上面的CGColor的地址与错误消息中的地址匹配.