【发布时间】:2009-12-19 05:31:03
【问题描述】:
如何在我的 NSButton 子类中添加按钮标题中的文本?
【问题讨论】:
标签: objective-c cocoa drawing subclass
如何在我的 NSButton 子类中添加按钮标题中的文本?
【问题讨论】:
标签: objective-c cocoa drawing subclass
这是我认为它可能的工作方式:
- (void) drawRect:(NSRect)aRect {
// other drawing commands
// ...
// other drawing commands
NSDictionary *att = nil;
NSMutableParagraphStyle *style =
[[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setLineBreakMode:NSLineBreakByWordWrapping];
[style setAlignment:NSCenterTextAlignment];
att = [[NSDictionary alloc] initWithObjectsAndKeys:
style, NSParagraphStyleAttributeName,
[NSColor whiteColor],
NSForegroundColorAttributeName, nil];
[style release];
[self.title drawInRect:self.bounds withAttributes:att];
[att release];
}
基于一些关于 iPhone 的 Objective-C 知识并查看
http://www.cocoadev.com/index.pl?DrawingABoundedString
您可能对网站示例使用静态变量感到满意,也可能不满意。
你可能猜到我不是。我的印象是当前的图形上下文隐含在调用中。
您可能希望根据按钮状态改变文本位置/颜色。
编辑
编辑代码以修复内存泄漏。
【讨论】:
您继承NSButtonCell,并覆盖NSButtonCell 和NSCell 可用的各种draw 方法中的一个或多个。
【讨论】: