0
votes

I am able to set clickable links on NSTextField elements, using NSAttributedString. The text shows correctly (blue coloured, underlined) as soon as the view appears.

However, the cursor shows on hover is the editing (I-Beam) cursor, until first click (anywhere) on the NSTextField. Once it is clicked, when hovering over the "hyperlinked" part of the text, the hand point cursor is shown as expected.

I was able to subclass NSTextField and override resetCursorRects, however, this allows me changing the cursor for the entire NSTextField bound, where I want it to show only when hovering over the hyperlinked parts of the text (Or to use cursorRects, but that seems as an overkill, to iterate on the (already iterated on creation) entire text).

I also tried setting the NSCursor attribute of the attributed string, but that did not solve the issue.

1
It looks like NSTextView does show the correct cursor and NSTextField doesn't.Willeke

1 Answers

3
votes

This is not directly supported by NSTextField, I believe. It starts working when you click in the textfield because at that point the field editor for the textfield is created; the field editor is an NSTextView, and that does what you want it to do. So you basically have two choices. You can (1) do the easy thing and use NSTextView instead of NSTextField (is there any particular reason why you can't?), or (2) you can set up the right cursor rects yourself in resetCursorRects by looping through the rects for the character ranges that have the link attribute set, but this means setting up a layout manager and so forth – work that is all done for you if you use NSTextView. A third possibility is to make the textfield's field editor come up right away, before the user clicks – in other words, make the textfield be the first responder. But that only works if you have only one such textfield in your window, and if nothing else in the window ever wants to be the first responder, so it is not a very satisfactory fix, probably.