Inset Text and Inner Shadow

Discussion in 'Mac Programming' started by WMuntean, Sep 16, 2011.

  1. WMuntean macrumors regular

    Aug 23, 2007
    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.
  2. kainjow Moderator emeritus


    Jun 15, 2000
    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.
  3. chown33 macrumors 604

    Aug 9, 2009
    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.
  4. WMuntean thread starter macrumors regular

    Aug 23, 2007
    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.

    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:

    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.
  5. kainjow Moderator emeritus


    Jun 15, 2000
    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*.
  6. WMuntean thread starter macrumors regular

    Aug 23, 2007
    Boom. Got it.

    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.

Share This Page