Q: I'm trying to use the pasteFont: method of the Text object to extract font information from the Font Pasteboard. The trouble is, when I try to do this, I always get the default font information--Helvetica 12pt. What am I doing wrong?
A: The solution is easy but perhaps not obvious. In order for the paste operation to have any effect, the Text object must contain a selection. It is not even strictly necessary that it contain any text. The following code illustrates the "low-pain" method of extracting font values from the Pasteboard. (The alternative "high-pain" method would involve parsing the rich text format from the Pasteboard):
#import <appkit/Text.h>
#import <appkit/Font.h>
id font, myText;
/* Allocate a temporary Text object */
myText = [[Text alloc] init];
/* It must allow multiple fonts -- i.e. rich text */
[myText setMonoFont: NO];
/* Stick in some text -- optional */
[myText setText:"hello"];
/* And select it -- any selection will do so long */
/* as something is selected */
[myText setSel:0 :1];
/* pasteFont: will copy the font info from the Pasteboard
/* to the font in the Text object */
[myText pasteFont:sender];
/* Do what you need to with the font */
font = [myText font];
/* Then free up the Text object */
[myText free];
QA677
Valid for 2.0, 3.0