objective c - OS X Status Bar Application with a title in two lines -
i'm old ios developer , want make simple os x status bar application. need put title @ nsstatusitem should in 2 lines, istatpro network feature.
how should add it?
here simple example. example shows 2 lines 2 simple blinking lights.
it uses nsview (custom view) 2 nstextfields , 2 nsimagewells inside of it.
the red , green light images added project , set image wells in ib.
.m
// // appdelegate.m // drawing strings // // created mark hunte on 07/10/2013. // copyright (c) 2013 mark hunte. rights reserved. // #import "appdelegate.h" @implementation appdelegate nsstatusitem *statusitem; -(void)awakefromnib{ //-- set attributed text lines. gives more control on text if want it. nsmutableparagraphstyle *paragraphstyle = [[nsmutableparagraphstyle alloc] init]; //--set height of lines paragraphstyle.maximumlineheight = 12.f; //-- text nstextfield 1 nsattributedstring *attributedstring = [[nsattributedstring alloc] initwithstring:@"line 1"attributes: [nsdictionary dictionarywithobjectsandkeys: [nscolor blackcolor], nsforegroundcolorattributename,paragraphstyle,nsparagraphstyleattributename ,nil]]; //-- text nstextfield 2 nsattributedstring *attributedstring2 = [[nsattributedstring alloc] initwithstring:@"line 2"attributes: [nsdictionary dictionarywithobjectsandkeys: [nscolor blackcolor], nsforegroundcolorattributename,paragraphstyle,nsparagraphstyleattributename ,nil]]; //--- set text [_textfield1 setattributedstringvalue:attributedstring]; [_textfield2 setattributedstringvalue:attributedstring2]; //--- set status bar nsstatusbar *bar = [nsstatusbar systemstatusbar]; statusitem = [bar statusitemwithlength: nsvariablestatusitemlength] ; //-- constrain custom views size [_customview setframesize: nsmakesize(50, 22)]; //--- add view status bar item [statusitem setview:_customview]; //-- make sure displays [ _customview display]; [_customview setneedsdisplay:true]; //-- hide 1 of image views [_greenlight sethidden:true]; } - (void)applicationdidfinishlaunching:(nsnotification *)anotification { //-- set time blink 2 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 hidden unhide it, if shown hide it. [_greenlight sethidden:![_greenlight ishidden]]; [_redlight sethidden:![_redlight ishidden]]; } @end
i use 2 textfields think give better control if needed. can use 1 , newline text. @"line 1\nline 2"
i had set maximum line hight text alignment , had fiddle constraints in ib.
but result 2 lines blinking lights:
Comments
Post a Comment