如何在 NSButton 子类中添加按钮标题中的文本?
最佳答案
我认为它可能是这样工作的:
- (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
您可能对网站示例中静态变量的使用感到满意,也可能不满意。
正如你可能猜到的那样,我不是。我的印象是当前图形上下文隐含在调用中。
您可能希望根据按钮状态改变文本位置/颜色。
编辑
编辑代码以修复内存泄漏。
关于objective-c - 在 NSButton 子类中绘制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1930650/