Code Monkey home page Code Monkey logo

Comments (5)

VladUsatii avatar VladUsatii commented on August 17, 2024 2

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.

from rumps.

cheerlessDreamer avatar cheerlessDreamer commented on August 17, 2024 1

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

from rumps.

dalleng avatar dalleng commented on August 17, 2024

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

from rumps.

lhl avatar lhl commented on August 17, 2024

Diego, thanks for the sample code! Super useful.

from rumps.

VladUsatii avatar VladUsatii commented on August 17, 2024

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

from rumps.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.