[英]OS X Status Bar Application with a title in two lines
我是老iOS開發人員,現在我想制作一個簡單的OS X狀態欄應用程序。 我需要在NSStatusItem上放置一個標題 ,但標題應該在兩行中,例如iStatPro網絡功能。
我應該如何添加?
這是一個非常簡單的示例。 此示例顯示兩行帶有兩個簡單的閃爍燈。
它使用一個NSView(自定義視圖),其中包含兩個NSTextField和兩個NSImageWell。
紅光和綠光圖像已添加到項目中,並設置為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
我使用兩個文本字段,因為我認為這將在需要時提供更好的控制。 但是您可以使用文本和換行符。 @“第1行\\ n第2行”
我還必須設置“最大行數”以幫助文本對齊,並且不得不擺弄IB中的約束。
但是結果是兩行燈閃爍:
子類化NSView,您可以在其中手動繪制文本(搜索繪制的NSString),然后將視圖添加到狀態項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.