• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Changing fonts without font menu?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Changing fonts without font menu?


  • Subject: Re: Changing fonts without font menu?
  • From: Mike Wright <email@hidden>
  • Date: Thu, 8 Jun 2006 17:36:50 -0500

On Jun 8, 2006, at 12:12, email@hidden wrote:
Date: Thu, 08 Jun 2006 16:48:58 +0200
From: Fredrik Olsson <email@hidden>
Subject: Re: Changing fonts without font menu?
To: Cocoa-dev <email@hidden>
Message-ID: <email@hidden>
Content-Type: text/plain; charset="iso-8859-1"

[...]
What I want to do is to let a NSButton wotk the same way as for example
the "Bold"-menu item that is "provided for free" with the Format-menu,
or the Font-menu.


I want to have a button on a form, hook it up to the shared font manager
and send it addFontTraits actions, having it selected and deselected
depending on the current selection.

Looking at the Format menu Bold and Italic items, it seems that - addFontTrait: requires a tag (2 for bold and 1 for italic?). The NSFontManager docs say:


"This action method causes the receiver to send its action message (changeFont: by default) up the responder chain."

and

"When a responder replies by providing a font to convert in a convertFont: message, the receiver [presumably the sharedFontManager] converts the font by adding the trait specified by sender. This trait is determined by sending a tag message to sender and interpreting it as a font trait mask for a convertFont:toHaveTrait: message."

If someone can tell me what this really means, I'd be very grateful. Who is it that sends a tag message to sender, how and when is the tag sent, and who is it that interprets it? The explanation in the docs looks a lot like English, but my tiny brain doesn't seem to be able to grasp its meaning.

Hooking up a button to send underline to the first responder is esy, and
works as expected (minus updating the selected status), but doing the
same with bold, italic, increase/decrease font size, have proved to be a
harder task.

Since I have trouble understanding a lot of the relevant documentation, including that for addFontTrait:, I skip around the problem by setting the Bold and Italic buttons in my app's toolbar to call the following actions, which simulate command-B and command-I, respectively.


This works because my Format menu Bold item uses commnand-B and the Italic item uses command-I. (I don't recall where I got them, but I'm pretty sure I didn't figure them out for myself.)

- (IBAction) toggleBold:(id)sender
{
     // Send Cmd-B Event
     CGPostKeyboardEvent((CGCharCode)0,(CGKeyCode)55,true );
     CGPostKeyboardEvent((CGCharCode)'B',(CGKeyCode)11,true );
     CGPostKeyboardEvent((CGCharCode)'B',(CGKeyCode)11,false );
     CGPostKeyboardEvent((CGCharCode)0,(CGKeyCode)55,false );
}


- (IBAction) toggleItalic:(id)sender { // Send Cmd-I Event CGPostKeyboardEvent((CGCharCode)0,(CGKeyCode)55,true ); CGPostKeyboardEvent((CGCharCode)'I',(CGKeyCode)34,true ); CGPostKeyboardEvent((CGCharCode)'I',(CGKeyCode)34,false ); CGPostKeyboardEvent((CGCharCode)0,(CGKeyCode)55,false ); }

The current docs say, "This function is not recommended for general use because of undocumented special cases and undesirable side effects. The recommended replacement for this function is CGEventCreateKeyboardEvent, which allows you to create a keyboard event and customize the event before posting it to the event system."

I suppose I'll look more closely at CGEventCreateKeyboardEvent and maybe change to use that, although I find the documentation almost completely opaque. CGEventCreateKeyboardEvent returns a keyboard event, which apparently needs to be posted somehow.

I thought that I might be able to use - keyEventWithType:location:modifierFlags:timestamp:windowNumber:context:c haracters:charactersIgnoringModifiers:isARepeat:keyCode: to create a keydown event and post it using NSApplication -sendEvent:, but I never got that to work.

Maybe someone will hate my solution so much that they'll post a better one.
__
Mike Wright
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden
  • Prev by Date: Re: NSComboBox -- How do I limit user typing to what is in the list
  • Next by Date: How to do modal mouse tracking loop efficiently?
  • Previous by thread: Re: Changing fonts without font menu?
  • Next by thread: Re: Changing fonts without font menu?
  • Index(es):
    • Date
    • Thread