OS X Status Bar Application with a title in two lines

我只是一个虾纸丫 提交于 2021-02-08 09:39:33

问题


I'm an old iOS developer and now I want to make a simple OS X status bar application. I need to put a title at the NSStatusItem but it should be in two lines, like iStatPro network feature.

How should I add it?


回答1:


Here is a very simple example. This example shows two lines with two simple blinking lights.

It uses a NSView (Custom view) with two NSTextFields and two NSImageWells inside of it.

The red and green light images are added to the project and set to the Image wells in IB.

enter image description here

enter image description here

.m

//
//  AppDelegate.m
//  Drawing Strings
//
//  Created by Mark Hunte on 07/10/2013.
//  Copyright (c) 2013 Mark Hunte. All rights reserved.
//

#import "AppDelegate.h"

@implementation AppDelegate
NSStatusItem *statusItem;

-(void)awakeFromNib{

    //-- SET UP ATTRIBUTED TEXT FOR THE LINES. WHICH GIVES US MORE CONTROL OVER THE TEXT IF WE WANT IT.
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];


    //--SET THE HEIGHT OF THE LINES
    paragraphStyle.maximumLineHeight = 12.f;


    //-- TEXT FOR NSTEXTFIELD 1
    NSAttributedString *attributedString = [[NSAttributedString alloc]
                                            initWithString:@"Line 1"attributes: [NSDictionary
                                                                                 dictionaryWithObjectsAndKeys: [NSColor blackColor], NSForegroundColorAttributeName,paragraphStyle,NSParagraphStyleAttributeName ,nil]];
     //-- TEXT FOR NSTEXTFIELD 2
    NSAttributedString *attributedString2 = [[NSAttributedString alloc]

                                             initWithString:@"Line 2"attributes: [NSDictionary
                                                                                  dictionaryWithObjectsAndKeys: [NSColor blackColor], NSForegroundColorAttributeName,paragraphStyle,NSParagraphStyleAttributeName ,nil]];


    //--- SET THE TEXT
    [_textField1  setAttributedStringValue:attributedString];
    [_textField2  setAttributedStringValue:attributedString2];


    //--- SET UP THE STATUS BAR
    NSStatusBar *bar = [NSStatusBar systemStatusBar];
    statusItem =  [bar statusItemWithLength: NSVariableStatusItemLength]  ;


    //-- CONSTRAIN THE CUSTOM VIEWS SIZE
    [_customView setFrameSize: NSMakeSize(50, 22)];


    //--- ADD THE VIEW TO THE STATUS BAR ITEM

    [statusItem setView:_customView];

    //-- MAKE SURE IT DISPLAYS
    [ _customView display];
    [_customView  setNeedsDisplay:TRUE];

    //-- HIDE ONE OF THE IMAGE VIEWS
    [_greenLight   setHidden:TRUE];


}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{


    //-- SET UP A TIME TO BLINK THE TWO IMAGE VIEW LIGHTS

NSTimer *    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(blinkLights) userInfo:nil repeats:YES];

    [timer fire];

}

-(void)blinkLights{

    //-- IF IMAGE VIEW IS HIDDEN UNHIDE IT, IF SHOWN HIDE IT.

    [_greenLight   setHidden:![_greenLight isHidden]];
    [_redLight   setHidden:![_redLight isHidden]];


}


@end

I use two textfields as I think this will give better control if needed. But you can use one and newline the text. @"Line 1\nLine 2"

I also had to set a Maximum line hight to help with the text alignment and had to fiddle with the constraints in IB.

But the result is two lines with blinking lights:

enter image description here

enter image description here




回答2:


Subclass an NSView where you draw your text manually (search for drawing NSString), then add the view to the Status Item.



来源:https://stackoverflow.com/questions/19211079/os-x-status-bar-application-with-a-title-in-two-lines

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