Hyperlinks in NSTextView
Hyperlinks in NSTextView
- Subject: Hyperlinks in NSTextView
- From: Saratchandra Kongara <email@hidden>
- Date: Mon, 25 Nov 2002 17:32:53 -0500
Hello,
I am trying to add hyperlinks to selected words in a NSTextView
control. The document is saved as RTFD (RTF with Attachments) to disk.
I got the links to work but after saving the document the link
attribute of the text is lost. When the document is re-opened and
displayed in NSTextView, clicking on the text is not opening the URL in
the default browser.
How can I save the document to preserve the link attribute across
invocations (multiple reads and writes to disk).
Here is the code
-----------------------
// Creates an NSAttributedString from a URL and Link text
- (NSAttributedString *)attributedStringWithLink:(NSString *)link
labelString:(NSString *)text {
NSDictionary *attrsDict;
attrsDict = [NSDictionary dictionaryWithObject:link
forKey:NSLinkAttributeName];
return[[[NSAttributedString alloc] initWithString:text
attributes:attrsDict] autorelease];
}
// Making the selected text a hyperlink
// keyView is an instance of NSTextView
NSRange range = [keyView selectedRange];
NSString *selectedString = [[keyView
attributedSubstringFromRange:range] string];
NSAttributedString *linkStr = [self
attributedStringWithLink:@"
http://www.google.com"
labelString:selectedString];
[[keyView textStorage] beginEditing];
[[keyView textStorage] replaceCharactersInRange:range
withAttributedString:linkStr];
[[keyView textStorage] endEditing];
// Saving data to file
[keyView writeRTFDToFile:@"data.rtfd" atomically:YES];
I tried saving in a different way, but not sure what to set the value
of keys in the dictionary for documentAttributes, in the code below.
NSRange range = NSMakeRange(0, [[keyView string] length]);
NSAttributedString *bodyString = [keyView
attributedSubstringFromRange:range];
NSFileWrapper *fileWrapper = [bodyString RTFDFileWrapperFromRange:range
documentAttributes:nil];
[fileWrapper writeToFile:@"data.rtfd" atomically:YES
updateFilenames:NO];
Any suggestions? Thanks.
Regards
Sarat Kongara
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.