带有两行标题的 OS X 状态栏应用程序 [英] OS X Status Bar Application with a title in two lines

查看:50
本文介绍了带有两行标题的 OS X 状态栏应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是老iOS开发人员,现在我想制作一个简单的OS X状态栏应用程序.我需要在NSStatusItem上放置一个 title ,但是它应该在两行中,例如 iStatPro 网络功能.

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.

我应该如何添加它?

推荐答案

这是一个非常简单的示例.本示例显示两行带有两个简单的闪烁灯.

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

它使用NSView(自定义视图),其中包含两个NSTextField和两个NSImageWell.

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

将红光和绿光图像添加到项目中,并设置到IB中的图像孔中.

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

我使用两个文本字段,因为我认为这将在需要时提供更好的控制.但是您可以使用 one 并换行文本.@第1行\ n第2行"

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"

我还必须设置最大行数"以帮助文本对齐,并且不得不摆弄IB中的约束.

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:

这篇关于带有两行标题的 OS X 状态栏应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆