简体   繁体   English

OS X状态栏应用程序,标题为两行

[英]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. 我是老iOS开发人员,现在我想制作一个简单的OS X状态栏应用程序。 I need to put a title at the NSStatusItem but it should be in two lines, like iStatPro network feature. 我需要在NSStatusItem上放置一个标题 ,但标题应该在两行中,例如iStatPro网络功能。

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. 它使用一个NSView(自定义视图),其中包含两个NSTextField和两个NSImageWell。

The red and green light images are added to the project and set to the Image wells in IB. 红光和绿光图像已添加到项目中,并设置为IB中的图像孔。

在此处输入图片说明

在此处输入图片说明

.m .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" @“第1行\\ n第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. 我还必须设置“最大行数”以帮助文本对齐,并且不得不摆弄IB中的约束。

But the result is two lines with blinking lights: 但是结果是两行灯闪烁:

在此处输入图片说明

在此处输入图片说明

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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