Highlighting URLs
Highlighting URLs
- Subject: Highlighting URLs
- From: Mark Woollard <email@hidden>
- Date: Thu, 9 Jan 2003 12:39:32 +0000
I'm trying to add some code to underline urls found in the contents of
a NSTextView control and allow clicking to open them. I've modified
some code I was pointed at a couple of months back to do this and it
correctly underlines and turns blue the urls. However it does not
appear to allow clicking, first the mouse doesn't change when moving
over the links and clicking on a link doesn't result in the activation
function at the end of the code being called. The code is in my
NSWindowController controller class and this class is the delegate for
the NSTextView in question. Can anyone give me an idea why this doesn't
work?
Thanks
Mark
- (void)hiliteAndActivateURLs:(NSTextView*)textView
{
NSTextStorage* textStorage=[textView textStorage];
NSString* string=[textStorage string];
NSString* str;
NSMutableCharacterSet *endSet = [NSMutableCharacterSet
whitespaceAndNewlineCharacterSet];
[endSet addCharactersInString:@">"];
NSRange searchRange=NSMakeRange(0, [string length]);
NSRange foundRange;
[textStorage beginEditing];
do {
//We assume that all URLs start with http://, www. or email:
foundRange=[string rangeOfString:@"
http://" options:0
range:searchRange];
if (foundRange.length == 0) foundRange = [string
rangeOfString:@"www." options:0 range:searchRange];
if (foundRange.length == 0) foundRange = [string
rangeOfString:@"email:" options:0 range:searchRange];
if (foundRange.length > 0) { //Did we find a URL?
NSURL* theURL;
NSDictionary* linkAttributes;
NSRange endOfURLRange;
//We assume the URL ends with whitespace
searchRange.location = foundRange.location + foundRange.length;
searchRange.length = [string length] - searchRange.location;
endOfURLRange=[string rangeOfCharacterFromSet:endSet
options:0 range:searchRange];
//The URL could also end at the end of the text. The next
line fixes it in case it does
if (endOfURLRange.location==0) endOfURLRange.location=[string
length];
endOfURLRange.location--;
searchRange.location = endOfURLRange.location;
searchRange.length = [string length]-searchRange.location;
//Set foundRange's length to the length of the URL
foundRange.length =
endOfURLRange.location-foundRange.location+1;
//grab the URL from the text
NSLog(@"%@",[string substringWithRange:foundRange]);
theURL=[NSURL URLWithString:[string
substringWithRange:foundRange]];
//Make the link attributes
linkAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
theURL, NSLinkAttributeName, [NSNumber
numberWithInt:NSSingleUnderlineStyle], NSUnderlineStyleAttributeName,
[NSColor blueColor], NSForegroundColorAttributeName, NULL];
//Finally, apply those attributes to the URL in the text
[textStorage addAttributes:linkAttributes range:foundRange];
}
} while (foundRange.length!=0); //repeat the do block until it no
longer finds anything
[textStorage endEditing];
}
- (BOOL)textView:(NSTextView*)textView clickedOnLink:(id)link
atIndex:(unsigned)charIndex {
BOOL success;
success=[[NSWorkspace sharedWorkspace] openURL: link];
return success;
}
_______________________________________________
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.