Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hyperlink in Cocoa

I am developing a Mac application in XCode. I need to add a hyperlink which navigates to a particular site. I tried this using a button, but I need to know how to change the cursor to hand cursor when mouse is over that button.

like image 596
Sreelal Avatar asked Jun 11 '09 08:06

Sreelal


2 Answers

I believe you can use a non-editable NSTextField to display the URL. If you set the attributes appropriately on the NSAttributedString that you set as the field's value property, it will display as the standard blue-with-underline and handle the cursor tracking for you. This Apple Q&A tells you how to set the attributes for a URL.

like image 160
Barry Wark Avatar answered Sep 22 '22 23:09

Barry Wark


To set the cursor, you have to use the cursor tracking method addCursorRect:cursor:. However, you are really only supposed to call that method from inside the resetCursorRects method. If you do it at any other time, it's basically guaranteed to be ignored.

So this means you need to subclass NSButton (or whatever NSView subclass you want to use) and override resetCursorRects to call addCursorRect:cursor: for the entire view's bounds.

like image 28
Alex Avatar answered Sep 25 '22 23:09

Alex