2010-10-14 93 views

回答

9

您可以嘗試屬性字符串值。

NSColor *txtColor = [NSColor redColor]; 
NSFont *txtFont = [NSFont boldSystemFontOfSize:14]; 
NSDictionary *txtDict = [NSDictionary dictionaryWithObjectsAndKeys: 
     txtFont, NSFontAttributeName, txtColor, NSForegroundColorAttributeName, nil]; 
NSAttributedString *attrStr = [[[NSAttributedString alloc] 
     initWithString:@"Hello!" attributes:txtDict] autorelease]; 
[[attrStrTextField cell] setAttributedStringValue:attrStr]; 
[attrStrTextField updateCell:[attrStrTextField cell]]; 
+2

您可以通過'0.0'大小來獲得默認的常規大小。您還可以向NSFont詢問任何其他尺寸(例如,小控件和單元格的尺寸)。你通常不應該傳遞一個明確的大小。 – 2010-10-14 15:53:22

12

試試這個,我認爲它是完美的。

NSColor *color = [NSColor redColor]; 
NSMutableAttributedString *colorTitle = 
    [[NSMutableAttributedString alloc] initWithAttributedString:[button attributedTitle]]; 

NSRange titleRange = NSMakeRange(0, [colorTitle length]); 

[colorTitle addAttribute:NSForegroundColorAttributeName 
        value:color 
        range:titleRange]; 

[button setAttributedTitle:colorTitle];