Get default font name in Cocoa for NSMenuItem?

社会主义新天地 提交于 2019-12-13 13:19:34

问题


I am using NSAttributed NSString in my NSMenuItem, but it's Font is changed as compare to default font, I wants to use default font for Attributed string.

Can any one explain, how to find or fetch the default Font for NSMenuItems.

Right now I am using this :

 NSDictionary *attributes = @{
                                 NSFontAttributeName: [NSFont fontWithName:@"Helvetica" size:14],
                                 NSForegroundColorAttributeName: [NSColor blackColor],
                                 NSParagraphStyleAttributeName:paragraphStyle

                                 };

Thanks


回答1:


To get the default font of an NSMenuItem just use the method [NSFont menuBarFontOfSize:0].

NSDictionary *attributes = @{ NSFontAttributeName: [NSFont menuBarFontOfSize:0],
                              NSForegroundColorAttributeName: [NSColor blackColor],
                              NSParagraphStyleAttributeName: paragraphStyle };

which is easier in all ways.




回答2:


I got the solution, I used this

NSFont *oldFont = [menu font];
NSLog(@"%@",oldFont.familyName);

and used like this

NSDictionary *attributes = @{
                                 NSFontAttributeName:[NSFont fontWithName:@"Lucida Grande" size:14],
                                 NSForegroundColorAttributeName: [NSColor blackColor],
                                 NSParagraphStyleAttributeName:paragraphStyle

                                 };


来源:https://stackoverflow.com/questions/22628267/get-default-font-name-in-cocoa-for-nsmenuitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!