开发者

Cocoa Drawing Clickable Text

开发者 https://www.devze.com 2023-03-15 14:36 出处:网络
I am trying to draw some text that is clickable. I am trying not to use NSButton as my text is being开发者_如何学运维 drawn in drawRect and using NSButton in a drawRect makes it add the subview on top

I am trying to draw some text that is clickable. I am trying not to use NSButton as my text is being开发者_如何学运维 drawn in drawRect and using NSButton in a drawRect makes it add the subview on top of itself unless you go through and remove all subviews on each call of drawRect.

I tried creating a NSButton and instead of adding it as a subview I am calling [myBtn.cell drawTitle: withFrame: inView:] which draws the text, but it is not clickable.

Is there a way to draw text (in a way like using drawInRect) but where I can also have the text be clickable?

If not, what would you recommend? Using a standard NSButton and clearing all the subviews each time?


One way I've seen this done before is to use an NSAttributedString. The part that's clickable should be changed somehow (ie, have a different NSForegroundColorAttributeName and NSUnderlineStyleAttributeName) and then attach the NSURL to it with the NSLinkAttributeName.

If I remember correctly, I believe this is an NSTextField subclass that does this: DSClickableURLTextField / Swift DSClickableURLTextField fork

I would recommend not drawing the text yourself, because there are lots of things in AppKit that will do this for you. Why do you need to draw it yourself?

0

精彩评论

暂无评论...
验证码 换一张
取 消