objective c - NSTextFieldCell with Multiple Lines -
i need show nstextfieldcell multiple lines different format on each line.
something this:
line 1: title
line 2: description
i subclassed nstextfieldcell don't know how go on it.
any ideas?
first off, don't have subclass nstextfieldcell
achieve this, since, subclass of nscell
, nstextfieldcell
inherits -setattributedstringvalue:
. string provided can represented nsattributedstring
. following code illustrates how achieve desired text ordinary nstextfield
.
mdappcontroller.h:
@interface mdappcontroller : nsobject <nsapplicationdelegate> { iboutlet nswindow *window; iboutlet nstextfield *textfield; } @end
mdappcontroller.m:
@implementation mdappcontroller static nsdictionary *regularattributes = nil; static nsdictionary *boldattributes = nil; static nsdictionary *italicattributes = nil; - (void)applicationdidfinishlaunching:(nsnotification *)anotification { if (regularattributes == nil) { regularattributes = [[nsdictionary dictionarywithobjectsandkeys: [nsfont systemfontofsize:[nsfont systemfontsize]],nsfontattributename, nil] retain]; boldattributes = [[nsdictionary dictionarywithobjectsandkeys: [nsfont boldsystemfontofsize:[nsfont systemfontsize]],nsfontattributename, nil] retain]; nsfont *regfont = [nsfont userfontofsize:[nsfont systemfontsize]]; nsfontmanager *fontmanager = [nsfontmanager sharedfontmanager]; nsfont *oblique = [fontmanager convertfont:regfont tohavetrait:nsitalicfontmask]; italicattributes = [[nsdictionary dictionarywithobjectsandkeys: oblique,nsfontattributename, nil] retain]; } nsstring *string = @"line 1: title\nline 2: description"; nsmutableattributedstring *rstring = [[[nsmutableattributedstring alloc] initwithstring:string] autorelease]; [rstring addattributes:regularattributes range:[string rangeofstring:@"line 1: "]]; [rstring addattributes:regularattributes range:[string rangeofstring:@"line 2: "]]; [rstring addattributes:boldattributes range:[string rangeofstring:@"title"]]; [rstring addattributes:italicattributes range:[string rangeofstring:@"description"]]; [textfield setattributedstringvalue:rstring]; } @end
this results in following:
now, depending on how intend text used, implement design in several different ways. may want whether nstextview
might work rather nstextfield
...
Comments
Post a Comment