12

I need to set a text size (for example to 42) of the selected rich text which uses multiple fonts.

I imagine I can check attributes of each group of characters, modify the font size and set attributes back, but looking at the floating Font panel it seems like there should be a very easy and straightforward way to accomplish that. Do I miss something obvious?

Indoor
  • 541
  • 4
  • 16

5 Answers5

11

On 10.6 there is a convenient way to iterate over the attributes and increase the font size. This method can be added to an NSTextView category.

    - (IBAction)increaseFontSize:(id)sender
{
    NSTextStorage *textStorage = [self textStorage];
    [textStorage beginEditing];
    [textStorage enumerateAttributesInRange: NSMakeRange(0, [textStorage length])
                                     options: 0
                                  usingBlock: ^(NSDictionary *attributesDictionary,
                                                NSRange range,
                                                BOOL *stop)
     {
#pragma unused(stop)
         NSFont *font = [attributesDictionary objectForKey:NSFontAttributeName];
         if (font) {
             [textStorage removeAttribute:NSFontAttributeName range:range];
             font = [[NSFontManager sharedFontManager] convertFont:font toSize:[font pointSize] + 1];
             [textStorage addAttribute:NSFontAttributeName value:font range:range];
         }
     }];
    [textStorage endEditing];
    [self didChangeText];

}
Jonathan Mitchell
  • 1,339
  • 12
  • 17
8

Generalizing on Jonathan's answer a bit, here is a category interface you can simply paste into appropriate files in your Xcode project:

@interface NSTextView (FrameworkAdditions)

-  (IBAction)decrementFontSize:(id)sender;
-  (IBAction)incrementFontSize:(id)sender;

@end

And the corresponding implementation:

@implementation NSTextView (FrameworkAdditions)

- (void)changeFontSize:(CGFloat)delta;
{
    NSFontManager * fontManager = [NSFontManager sharedFontManager];
    NSTextStorage * textStorage = [self textStorage];
    [textStorage beginEditing];
    [textStorage enumerateAttribute:NSFontAttributeName
                            inRange:NSMakeRange(0, [textStorage length])
                            options:0
                         usingBlock:^(id value,
                                      NSRange range,
                                      BOOL * stop)
     {
         NSFont * font = value;
         font = [fontManager convertFont:font
                                  toSize:[font pointSize] + delta];
         if (font != nil) {
             [textStorage removeAttribute:NSFontAttributeName
                                    range:range];
             [textStorage addAttribute:NSFontAttributeName
                                 value:font
                                 range:range];
         }
     }];
    [textStorage endEditing];
    [self didChangeText];
}

-  (IBAction)decrementFontSize:(id)sender;
{
    [self changeFontSize:-1.0];
}

-  (IBAction)incrementFontSize:(id)sender;
{
    [self changeFontSize:1.0];
}

@end
Kaelin Colclasure
  • 3,925
  • 1
  • 26
  • 36
4

This will double the font size, but you may change the scale property to any value, or provide your fixed size

    NSFont * font = ...;
    CGFloat fontSize =  [[font fontDescriptor].fontAttributes[NSFontSizeAttribute] floatValue];
    font = [NSFont fontWithDescriptor:[font fontDescriptor] size:fontSize * 2.];

    self.textField.font = font;
Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179
  • This worked for me and looks simpler than the other answers. Here's a Swift 3 version `font = NSFont.init(descriptor: font.fontDescriptor, size: ...)` – Alex May 18 '17 at 11:57
2

Note: I assume that you are using a NSTextView and that you can access its text storage (NSTextStorage).

I think it is not possible to only change the font's size over a text that use multiple fonts. In NSAttributedString, font's size is part of the NSFontAttributeName attribute which controls both the font and the size.

One solution is to iterate over the selection and use the attribute:atIndex:longestEffectiveRange:inRange: to capture the range when each font apply, change the font's size and then use the addAttribute:value:range: to set the new font over the range.

Update:

If you take a look at the GNUstep GUI source code for NSTextView (under LGPL), you will see that their implementation use the range iteration.

Laurent Etiemble
  • 27,111
  • 5
  • 56
  • 81
  • 1
    Yes, that was the first thing that came into my mind. But there's already a mechanism for making text bigger or smaller. If some menu item or slider has a tag of 3 (or 4) and sends modifyFont: message to NSFontManager, then NSFontManager talks to NSTextView and change the text size to a lower or bigger value (and preserving font, style, etc). It is done automatically. And so I think I'd like to hook into this mechanism, but just don't see where I can do it. If it's possible at all. – Indoor Feb 11 '10 at 17:19
  • There is a built-in mecanism that works only between NSText instance and NSFontManager (See http://developer.apple.com/Mac/library/documentation/Cocoa/Conceptual/FontHandling/Tasks/RespondingToFontChanges.html#//apple_ref/doc/uid/20000441). Basically, when the FontPanel change the font (size or trait), it sends a changeFont: message to the responder chain. If a responder is found, it sends back a convertFont: message to the FontManager for each range of text. I am afraid that you have to replicate what is done inside if you don't use the FontPanel... – Laurent Etiemble Feb 12 '10 at 09:43
  • Yes, seems so. I ended up using attribute:atIndex:longestEffectiveRange:inRange: and convertFont:toSize: But it's strange to me there's no a simple method for setting font size (considering there're ways for increasing or decreasing font size, which I can use, but I just don't need it). Thank you, Laurent. – Indoor Feb 12 '10 at 22:08
1

Since NSTextView is a subclass of NSView, you can use -scaleUnitSquareToSize: to change the magnification level of the text view. For example, to make all the text double sized you'd call:

[textView scaleUnitSquareToSize:NSMakeSize(2.0, 2.0)];

You may need to make some adjustments to the dimensions of the text view's NSTextContainer after performing this operation to ensure the text is laid out correctly.

Rob Keniger
  • 45,830
  • 6
  • 101
  • 134
  • I just need to really change font size, not to scale a view, but thanks anyway, I didn't know that method existed. – Indoor Feb 12 '10 at 22:11
  • 1
    @Rob this is great! It's precisely what was needed instead of what I *thought* would be needed (i.e., enumerating over font attributes to change their relative size, which would likely annoy users and create other problems). – Dalmazio Oct 08 '15 at 23:50