2013-10-06 3 views
1

저는 오래된 iOS 개발자이며 이제는 간단한 OS X 상태 표시 줄 응용 프로그램을 만들고 싶습니다. 제목을으로 NSStatusItem에 넣어야하지만, iStatPro 네트워크 기능과 같이 두 줄로 입력해야합니다.OS X 상태 표시 줄 제목이 두 줄로 된 응용 프로그램

어떻게 추가해야합니까?

+0

내가 아니라고 맥에서 내가 당신을 위해 함께 예를 넣을 수 있습니다 :

는하지만 결과는 깜박이는 불빛과 함께 두 줄입니다. 그러나 기본적으로 사용자 정의 NSView를 NSStatusItem에 추가합니다. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSStatusItem_Class/Reference/Reference.html#//apple_ref/doc/uid/20000248-BBCFFDJA를 참조하십시오. – markhunte

답변

0

텍스트를 수동으로 그려 NSView를 서브 클래 싱 한 다음 (NSString 그리기 검색) 상태 항목에보기를 추가하십시오.

1

다음은 매우 간단한 예입니다. 이 예에서는 두 개의 간단한 깜박임 표시등이있는 두 줄을 보여줍니다.

두 개의 NSTextField와 두 개의 NSImageWell이있는 NSView (사용자 정의보기)를 사용합니다.

빨강 및 초록 빛 이미지가 프로젝트에 추가되고 IB의 이미지 우물로 설정됩니다. enter image description here

// 
// 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 

하는 .m

내가 필요하다면이 더 나은 제어를 제공합니다 생각하는 나는 두 개의 텍스트 필드를 사용

enter image description here

. 그러나 하나와 개행 문자를 사용할 수 있습니다. @ "Line 1 \ nLine 2"

또한 텍스트 정렬에 도움이되는 최대 줄 높이를 설정해야하고 IB의 제약 조건을 가지고 있어야했습니다.

enter image description here

enter image description here