Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well here's my excuse for needing multiline buttons: I'm writing an emulator for an IBM 701, complete with front panel, and, bless their hearts, the designers of that front panel used multi-line labels. Here's my code. You only have to subclass NSButtonCell (not NSButton), and only one method needs to be overridden.</p> <pre><code>// In Xcode 4.6 (don't know about earlier versions): Place NSButton, then double-click it // and change class NSButtonCell to ButtonMultiLineCell. @interface ButtonMultiLineCell : NSButtonCell @end @implementation ButtonMultiLineCell - (NSRect)drawTitle:(NSAttributedString *)title withFrame:(NSRect)frame inView:(NSView *)controlView { NSAttributedString *as = [[NSAttributedString alloc] initWithString:[title.string stringByReplacingOccurrencesOfString:@" " withString:@"\n"]]; NSFont *sysFont = [NSFont systemFontOfSize:10]; NSMutableParagraphStyle *paragraphStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease]; [paragraphStyle setAlignment:NSCenterTextAlignment]; NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: sysFont, NSFontAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil]; NSSize textSize = [as.string sizeWithAttributes:attributes]; NSRect textBounds = NSMakeRect(0, 0, textSize.width, textSize.height); // using frame argument seems to produce text in wrong place NSRect f = NSMakeRect(0, (controlView.frame.size.height - textSize.height) / 2, controlView.frame.size.width, textSize.height); [as.string drawInRect:f withAttributes:attributes]; return textBounds; // not sure what rectangle to return or what is done with it } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload