views:

240

answers:

2

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?

+1  A: 

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
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
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
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
A: 

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
I just need to really change font size, not to scale a view, but thanks anyway, I didn't know that method existed.
Indoor