Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

WMuntean

macrumors regular
Original poster
Aug 23, 2007
178
0
Hey guys, I've been trying to get create inner shadows on text and I was wondering on whats the best method of doing this. Would it be to subclass NSTextField and rewrite the drawRect method? Thanks.
 
Since shadows just get applied to the context, you could subclass NSTextFieldCell, override the draw method, set your shadow, then call the super draw method. I've done that for a generic NSTextFieldCell that adds a normal shadow.
 
What's your Cocoa experience?

What other standard Cocoa classes have you subclassed and overridden drawing for? How familiar are you with the low-level text rendering system?

Do you have an algorithm for inset text and inner shadow?


Depending on the overall context of what you're trying to do, I'm wondering if a webview with CSS-styled text might be a better approach. Googling the title of this thread shows a number of CSS solutions (or possible solutions), including posts on stackoverflow and other Q&A sites.

Overriding NSTextField's drawing code is almost certainly the last thing on my list of things to try. Second-to-last is probably "Find another visual effect" because inset text with inner shadow is too damn complicated to be worth the effort.
 
Since shadows just get applied to the context, you could subclass NSTextFieldCell, override the draw method, set your shadow, then call the super draw method. I've done that for a generic NSTextFieldCell that adds a normal shadow.

I think this method to draw an inner shadow on the entire cell, but wasn't successful in drawing an inner shadow on just the text. I think you have to subclass NSTextField and not NSTextFieldCell. But, again, I'm not too sure.

What's your Cocoa experience?

What other standard Cocoa classes have you subclassed and overridden drawing for? How familiar are you with the low-level text rendering system?

Do you have an algorithm for inset text and inner shadow?


Depending on the overall context of what you're trying to do, I'm wondering if a webview with CSS-styled text might be a better approach. Googling the title of this thread shows a number of CSS solutions (or possible solutions), including posts on stackoverflow and other Q&A sites.

Overriding NSTextField's drawing code is almost certainly the last thing on my list of things to try. Second-to-last is probably "Find another visual effect" because inset text with inner shadow is too damn complicated to be worth the effort.

I'm quite new to Cocoa, but am making my way through. I'm not too familiar with low-level text rendering, but I'm willing to learn. I would rather not use a webview if I don't have to. I've implemented this technique in Cocoa Touch:

http://stackoverflow.com/questions/3231690/inner-shadow-in-uilabel

but now I'm having some difficulty porting it to Cocoa. Any help/direction would be much appreciated. I'm finding that documentation on Cocoa is seemingly sparse.
 
I think this method to draw an inner shadow on the entire cell, but wasn't successful in drawing an inner shadow on just the text. I think you have to subclass NSTextField and not NSTextFieldCell. But, again, I'm not too sure.

NSTextField is just the NSControl that holds the NSTextFieldCell and handles events. The cell is what does the drawing, and is what's reused elsewhere, like in an NSTableView or NSMatrix (same applies for button).

What are you having trouble with with porting that iOS code? All the CG* stuff should just work, you just have to know how to get the CGContextRef from a Cocoa graphics context, and the replacements for UIGraphics* functions - see CGBitmapContext*.
 
NSTextField is just the NSControl that holds the NSTextFieldCell and handles events. The cell is what does the drawing, and is what's reused elsewhere, like in an NSTableView or NSMatrix (same applies for button).

Boom. Got it.

What are you having trouble with with porting that iOS code? All the CG* stuff should just work, you just have to know how to get the CGContextRef from a Cocoa graphics context, and the replacements for UIGraphics* functions - see CGBitmapContext*.

Thanks for the pointers. I'm having trouble replacing some of the UIGraphics functions appropriately. When I have time I'll post which functions are giving me issues, maybe you can help.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.