Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to select the font for title? #30

Open
lhl opened this issue Jan 15, 2015 · 5 comments
Open

Is it possible to select the font for title? #30

lhl opened this issue Jan 15, 2015 · 5 comments

Comments

@lhl
Copy link

lhl commented Jan 15, 2015

I just wrote a quick countdown timer app in rumps - awesome work, really was a cinch!

The one question I had is how easy/possible it might be to change the font (ideally to a monospaced font like Monaco)? Some menubar apps seem able to do it, but I don't know enough of the Menubar/Cocoa specifics to know if that's something that's super hacky or not.

@dalleng
Copy link

dalleng commented Jan 17, 2015

It can de done by using NSAttributedString which let's you define strings with attributes such as font, background color, foreground color, underline, etc. To see all options check the docs. The MenuItem class rumps provides is a wrapper for AppKit's NSMenuItem which has a attributedTitle property, rumps does not provide a way to set it so you'll need to access MenuItem's _menuitem internal variable and use some PyObjC. Here's a sample:

import rumps

from AppKit import NSAttributedString
from PyObjCTools.Conversion import propertyListFromPythonCollection
from Cocoa import (NSFont, NSFontAttributeName,
    NSColor, NSForegroundColorAttributeName)

if __name__ == "__main__":
    font = NSFont.fontWithName_size_("Monaco", 18.0)
    color = NSColor.redColor()

    """
    Dictionary of attributes
    propertyListFromPythonCollection is used to create NSDictionary from a python dict
    conversionHelper=lambda x: x is used because font and color are alreaady cocoa objects
    and do not need to be converted
    """
    attributes = propertyListFromPythonCollection({
        NSFontAttributeName: font,
        NSForegroundColorAttributeName: color}
    , conversionHelper=lambda x: x)

    string = NSAttributedString.alloc().initWithString_attributes_("Foo", attributes)
    menu_item = rumps.MenuItem("")
    menu_item._menuitem.setAttributedTitle_(string)
    menu = [menu_item]

    app = rumps.App("Sample", menu=menu)
    app.run()

screenshot 2015-01-16 22 25 07

@lhl
Copy link
Author

lhl commented Jan 17, 2015

Diego, thanks for the sample code! Super useful.

@lhl lhl closed this as completed Jan 17, 2015
@jaredks jaredks reopened this Jan 29, 2015
@VladUsatii
Copy link
Contributor

How would one bold the font here? Or italicize? I grep'd NSFont in Terminal and can't find anything useful.

@cheerlessDreamer
Copy link
Contributor

cheerlessDreamer commented Jun 21, 2021

How would one bold the font here? Or italicize? I grep'd NSFont in Terminal and can't find anything useful.

The font used in the sample (Monaco) doesn't actually have any options apart from the regular weight, at least on my machine. You can use the following line as a test instead:

font = NSFont.fontWithName_size_("Helvetica", 18.0)

… and for a bold and italic version:

font = NSFont.fontWithName_size_("Helvetica Bold Oblique", 18.0)

You can get the correct name by using the 'Font Book' application.

EDIT: Note that using Dark Mode alters the appearance somewhat, see attached screenshot for an example. The red in the sample code appears pink under Dark Mode.
Screenshot 2021-06-21 at 14 12 20

@VladUsatii
Copy link
Contributor

Thanks for explaining Bold and Oblique, Cheerless Dreamer.

I did notice (in regards to your EDIT) that NSColor appears different in dark mode if you don't specify the alpha (the opacity). By default, the alpha is set to 0.5 for the rumps module.

Change the following:

color = NSColor.redColor()

to:

color = NSColor.colorWithCalibratedRed_green_blue_alpha_(1, 0, 0, 1)

And now, when you go into Dark Mode, it is completely red.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants