[英]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.