You can set font size and style for the current selection by calling the
TXNSetTypeAttributes
function. The current selection can be a range of text selected by the user or specified by your application. You specify the current selection with the
iStartOffset
and
iEndOffset
parameters of the
TXNSetTypeAttributes
function.
MLTE stores font attributes in a
TXNTypeAttributes
structure. This structure contains the
TXNAttributeData
union, and it is that union you use to pass attribute values. The
tag
field of the
TXNTypeAttributes
structure defines the type of data in the
TXNAttributeData
union, and the
size
field of the
TXNTypeAttributes
structure defines the size of the data in the
TXNAttributeData
union.
Typically your application would have a function that handles menu item selections. If a user makes a selection from the Size or Style menu, you change the appropriate values in the
TXNTypeAttributes
structure, then you call the
TXNSetTypeAttributes
function to change the attributes for the current selection.
The code fragment in Listing 3-9 shows how your application can handle size and style changes within a function that handles menu item selections.
Listing 3-9 Handling changes made in the Size and Style menus
TXNObject textObject; TXNTypeAttributes typeAttr; case mSize: { static short aFontSizeList[] = {9, 10, 12, 14, 18, 24, 36}; short shortValue = aFontSizeList[menuItem - 1]; // Assign the size attributes. typeAttr.tag = kTXNQDFontSizeAttribute; typeAttr.size = kTXNFontSizeAttributeSize; typeAttr.data.dataValue = shortValue << 16; // Use the MLTE function to set the size attributes. status = TXNSetTypeAttributes (textObject, 1, &typeAttr, kTXNUseCurrentSelection, kTXNUseCurrentSelection); // If there is an error if (status != noErr) // call your own error handling function. MyAlertUser (eNoFontSize); break; case mStyle: { Style newStyle; // Assign the new style based on the menu item // the user chooses. switch (menuItem) { case iPlain: newStyle = normal; break; case iBold: newStyle = bold; break; case iItalic: newStyle = italic; break; case iUnderline: newStyle = underline; break; case iOutline: newStyle = outline; break; case iShadow: newStyle = shadow; break; case iCondensed: newStyle = condense; break; case iExtended: newStyle = extend; break; default: break; } // Assign the style attributes. typeAttr.tag = kTXNQDFontStyleAttribute; typeAttr.size = kTXNQDFontStyleAttributeSize; typeAttr.data.dataValue = newStyle; // Use the MLTE function to set the style attributes. status = TXNSetTypeAttributes(textObject, 1, &typeAttr, kTXNUseCurrentSelection, kTXNUseCurrentSelection); // If there is an error if (status != noErr) // call your own error handling function. MyAlertUser (eNoFontStyle); break;