Making links clickable errors
Making links clickable errors
- Subject: Making links clickable errors
- From: deekpyro <email@hidden>
- Date: Sun, 07 Apr 2002 21:58:54 -0500
I'm trying to make my links clickable, but I get refered to a page that
although it has all the info I need has many typos. I'm confused by the
typos and try to fix them, but they are so many they are mind blowing! Plz
help!
- (void)hiliteAndActivateURLs:(NSTextView*)textView
{
NSTextStorage* textStorage=[textView textStorage];
NSString* string=[textStorage string];
NSRange searchRange=NSMakeRange(0, [string length];
NSRange foundRange;
[textStorage beginEditing];
do {
//We assume that all URLs start with http://
foundRange=[string rangeOfString:@"
http://" options:0
range:searchRange];
if (foundRange.length > 0) { //Did we find a URL?
NSURL* theURL;
NSDictionary* linkAttributes;
NSRange endOfURLRange;
//Restrict the searchRange so that it won't find the same string
again
searchRange.location=foundRange.location+foundRange.length;
searchRange.length = [string length]-searchRange.location;
//We assume the URL ends with whitespace
endOfURLRange=[string rangeOfCharacterFromSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]
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]-1;
//Set foundRange's length to the length of the URL
foundRange.length =
endOfURLRange.location-foundRange.location+1;
//grab the URL from the text
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];
}
^-- that's what I found
Thanks, Derek
_______________________________________________
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.