0

此代码不绘制白色文本,为什么?

NSMutableParagraphStyle* style = [[NSMutableParagraphStyle alloc] init];
[style setAlignment:NSCenterTextAlignment];
NSFont *font = [NSFont fontWithName:@"System" size:13];
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:style, NSParagraphStyleAttributeName, font, NSFontAttributeName, [NSColor whiteColor], NSForegroundColorAttributeName, nil];

[button.title drawInRect:textRect withAttributes:attrs];
4

1 回答 1

3

(Assuming the [cocoa] tag doesn't mean cocoa touch)

It's because NSButton is likely overriding the choice you've made to draw it's text in when it's -drawRect: gets called again. You can apply the attributes you've given in that dictionary to an NSAttributedString and call setAttributedTitle: to keep your style choices around.

If you need more fine-grain control over text rendering, either edit and move your logic into -drawRect: if it isn't already there, or provide an NSTextField or NSTextView as appropriate.

The main problem with the code you've provided is that @"System" isn't a font name.

于 2013-03-09T00:37:34.513 回答