Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / October 2005



Tip: Looking for answers? Try searching our database.

set default font how?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
None - 12 Oct 2005 03:46 GMT
Ugh, sorry for this stupid newbie question, but I just can't seem to
get this to work the way I want. I would like to be able to initially
set the font of a new text view, and also to be able to
programmatically change the leading. I do not particularly need or want
it to apply to only part of the text, but it should apply to all, and
also carry on when new text is typed in by the user.

I have in my awakeFromNib routine for the NSTextView subclass:

  NSTextStorage *storage = [self textStorage];
  [storage addAttribute:NSFontAttributeName value:[NSFont
fontWithName:@"Courier" size:12] range:[self
rangeForUserParagraphAttributeChange]];

and that seems to do nothing at all, and I did verify that the
awakeFromNib is being called .

Next problem is when I later change the font, by adding a
NSFontAttributeName attribute with the appropriate font to the
textStorage of the NSTextView. This part actually works, but even if I
apply it to the entire range, new letters typed in do not pickup the
new font; they go back to the default font.

Any ideas on a way to fix these?

I'm thinking maybe I should be approaching the setDefaultParagraphStyle
command, but I can't see a way to get the Font info into a
NSParagraphStyle object....

thanks

--
Allen Brunson - 12 Oct 2005 04:29 GMT
> Ugh, sorry for this stupid newbie question, but I just can't seem to
> get this to work the way I want.

i found it to be more "challenging" than i'd hoped, as well.

> I would like to be able to initially set the font of a new text view,

NSTextView is bad about font changes not "sticking," where sometimes it will
just randomly revert to its default font.  i have a vague memory of having
stumbled across a web page where somebody said this was a known issue, but i
can't recall the details.  so what i did was derive my own class from
NSTextView, and override the [NSTextView font] and [NSTextView setFont:]
methods with my own.  they get and set my custom NSFont pointer, which i made
a member of my class.  in the getter, if the font pointer is nil, i.e., this
is the first call, i load it up with a default font.

so, the first step to setting a default font is to make up your own custom
font pointer in your subclass, then call [NSTextView setFont:] with your
preferred font.  you might want to load this from prefs, so the user doesn't
have to keep setting it over and over.  NSTextView has hooks so that it can
automatically interact with the font picker window, so all you have to do is
give the user a menu option that brings up the font picker, and you've got
custom font selection out of the way.

when appending text to such a view, you have to do a little trickery, to make
sure the font will still be correct.  i added this method to my custom
NSTextView subclass:

 -(void)textAppend:(NSString*)text
   {
     NSFont*           font = [self font];
     NSTextStorage*    nsts = [self textStorage];
     NSMutableString*  nstr = [nsts mutableString];

     [nstr appendString:text];
     if (font) [nsts setFont:font];
   }

> I do not particularly need or want it to apply to only part of the text,
> but it should apply to all, and  also carry on when new text is typed in
> by the user.

if you do all the stuff i've mentioned here, your requirements will be met.

> and also to be able to programmatically change the leading.

can't help you with that one, sorry.  if i wanted to do that, i'd examine
NSLayoutManager.  i note that is has a defaultLineHeightForFont: method that
you might be able to override.  or perhaps there is some attribute you can
apply to the NSTextStorage object inside the NSTextView.
Michael Ash - 12 Oct 2005 06:49 GMT
> Ugh, sorry for this stupid newbie question, but I just can't seem to
> get this to work the way I want. I would like to be able to initially
[quoted text clipped - 12 lines]
> and that seems to do nothing at all, and I did verify that the
> awakeFromNib is being called .

It certainly would do nothing, because there are no characters in the
NSTextStorage that this attribute could apply to. NSAttributedString does
not hold zero-range attributes.

> Next problem is when I later change the font, by adding a
> NSFontAttributeName attribute with the appropriate font to the
> textStorage of the NSTextView. This part actually works, but even if I
> apply it to the entire range, new letters typed in do not pickup the
> new font; they go back to the default font.

You're conflating two separate concepts here, one of which is the idea of
setting attributes for existing text, and the other of which is setting
attributes for new text. This is perfectly understandable, of course,
because the GUI makes it look like they're the same, but under the hood
they're completely unrelated.

You already know how to set attributes for existing text. To set the
attributes for newly-typed text, use NSTextView's setTypingAttributes:
method. Note that it resets every time the selection is changed, so if you
want to hold it constant you'll have to manually re-reset it in that case.

Signature

Michael Ash
Rogue Amoeba Software

None - 13 Oct 2005 02:12 GMT
Thanks, both, for very helpful comments. I will need to use both
approaches for my project. I appreciate the help
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.