Re: NSTextView - changing font size
Re: NSTextView - changing font size
- Subject: Re: NSTextView - changing font size
- From: Jeremy Dronfield <email@hidden>
- Date: Thu, 11 Jul 2002 11:25:21 +0100
Thanks very much. It works perfectly. Two comments, though: the variable
nextRange isn't used (I presume that was an oversight?); and the
compiler wouldn't recognise the constant NSZeroRange. I found it in the
Cocoa documentation (where it's referred to as ZeroRange, without the
'NS'), but it isn't anywhere in the Foundation docs, nor in NSRange.h.
Has Cocoa discarded this constant, do you think? As an alternative, I've
set location and length to 0 the longhand way. Thanks for your help.
- Jeremy
On Wednesday, July 10, 2002, at 09:38 pm, Brian Webster wrote:
There may be a simpler way to do this, but this should work, assuming
that all the right sizes/styles of your fonts are installed on the
system, of course.
-(void)sizeText
{
NSTextStorage *textStorage = [textView textStorage];
NSRange range, nextRange;
NSFont *oldFont, *newFont;
NSFontManager *fontManager = [NSFontManager sharedFontManager];
range = NSZeroRange;
while(NSMaxRange(range) < [textStorage length])
{
NSDictionary *attributes = [textStorage
attributesAtIndex:NSMaxRange(range) effectiveRange:&range];
oldFont = [attributes objectForKey:NSFontAttributeName];
newFont = [fontManager convertFont:newFont toSize:sizeOption];
[textStorage addAttribute:NSFontAttributeName value:newFont
range:range];
}
}
I typed this code in Mail, so it's probably not quite right, but I
think it gets the idea across. You basically have to set the font on a
per-style run basis, so that each range of characters will have the
same traits as before, but a different size. Oh, and sending
beginEditing and endEditing to the text storage before and after the
loop may give you a performance increase, since it will only do all its
recalculations once that way.
--
Brian Webster
email@hidden
http://homepage.mac.com/bwebster
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.