2014-02-10 162 views
6

我爲此保留的每個資源都是針對iOS的。我似乎無法爲Mac OSX正常工作。任何想法如何獲得這個設置字體大小以及?它目前正確設置顏色和中心對齊。由於NSMutableAttributedString設置字體大小

NSMutableAttributedString *libTitle = [[NSMutableAttributedString alloc] initWithString:@"Library"]; 
[libTitle addAttribute:NSForegroundColorAttributeName value:[NSColor whiteColor] range:NSMakeRange(0,[@"Library" length]) ]; 
[libTitle setAlignment:NSCenterTextAlignment range:NSMakeRange(0, [@"Library" length])]; 
[libTitle addAttribute:NSFontSizeAttribute value:[NSFont systemFontOfSize:18.0] range:NSMakeRange(0, [@"Library" length])]; 

[self.libbtn setAttributedTitle:libTitle]; 

回答

11

嘗試設置你的字體屬性從一開始走,在init方法:

NSFont *systemFont = [NSFont systemFontOfSize:18.0f]; 
NSDictionary * fontAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:systemFont, NSFontAttributeName, nil]; 
NSMutableAttributedString *libTitle = [[NSMutableAttributedString alloc] initWithString:@"Library" attributes:fontAttributes]; 
NSRange rangeOfTitle = NSMakeRange(0,[libTitle length]); 
[libTitle addAttribute:NSForegroundColorAttributeName value:[NSColor whiteColor] range:rangeOfTitle]; 
[libTitle setAlignment:NSCenterTextAlignment range:rangeOfTitle]; 

[self.libbtn setAttributedTitle:libTitle]; 
+2

如果你想爲iOS做到這一點:改變NSFont到UIFont –