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?

5 Answers 5

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];

}
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
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;
1
  • 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, 2017 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.

3
  • 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, 2010 at 17:19
  • There is a built-in mecanism that works only between NSText instance and NSFontManager (See developer.apple.com/Mac/library/documentation/Cocoa/Conceptual/…). 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... Feb 12, 2010 at 9: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, 2010 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.

2
  • 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, 2010 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 8, 2015 at 23:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.