简体   繁体   中英

OS X Status Bar Application with a title in two lines

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?

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.

在此处输入图片说明

在此处输入图片说明

.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:

在此处输入图片说明

在此处输入图片说明

子类化NSView,您可以在其中手动绘制文本(搜索绘制的NSString),然后将视图添加到状态项。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM