Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [88]

By Root 823 0

}

Now, when you lose first-responder status, you need to redraw the view and the area occupied by the fuzzy blue glow around it:

- (BOOL)resignFirstResponder

{

NSLog(@"Resigning");

[self setKeyboardFocusRingNeedsDisplayInRect:[self bounds]];

return YES;

}

Build and run your application.

Chapter 20. Drawing Text with Attributes


The next step is to get the string to appear in our view. At the end of this chapter, your application will look like Figure 20.1. The character being displayed will change as you type.

Figure 20.1. Completed Application

NSFont


Overall, the class NSFont has only two types of methods:

1. Class methods for getting the font you want

2. Methods for getting metrics on the font, such as letter height

The following are commonly used methods in NSFont:

+ (NSFont *)userFontOfSize:(float)fontSize

+ (NSFont *)userFixedPitchFontOfSize:(float)fontSize

+ (NSFont *)messageFontOfSize:(float)fontSize

+ (NSFont *)toolTipsFontOfSize:(float)fontSize

+ (NSFont *)titleBarFontOfSize:(float)fontSize

Return a font object for the user’s default font for the corresponding string types. If you send a fontSize of 0.0, these methods will use the default font size.

User fonts are intended to be used in areas representing user input: a text field, for example. The other methods are useful when implementing custom user interface controls.

+ (NSFont *)fontWithName:(NSString *)fontName size:(float)fontSize

Returns a font object; fontName is a family-face name, such as “HelveticaBoldOblique” or “Times-Roman.” Again, a fontSize of 0.0 uses the default font size.

Unless your application calls for using a specific font, we recommend using the prior set of methods in place of this one, in order to maintain consistency.

NSAttributedString


Sometimes, you want to display a string that has certain attributes for a range of characters. As an example, suppose that you want to display the string “Big Nerd Ranch” and want the letters 0 through 2 to be underlined, the letters 0 through 7 to be green, and the letters 9 through 13 to be subscripts.

When dealing with a range of numbers, Cocoa uses the struct NSRange. NSRange has two members: location and length are both integers. The location is the index of the first item, and the length is the number of items in the range. You can use the function NSMakeRange() to create an NSRange.

To create strings with attributes that remain in effect over a range of characters, Cocoa has NSAttributedString and NSMutableAttributedString. Here is how you could create the NSAttributedString just described:

NSMutableAttributedString *s;

s = [[NSMutableAttributedString alloc]

initWithString:@"Big Nerd Ranch"];

[s addAttribute:NSFontAttributeName

value:[NSFont userFontOfSize:22]

range:NSMakeRange(0, 14)];

[s addAttribute:NSUnderlineStyleAttributeName

value:[NSNumber numberWithInt:1]

range:NSMakeRange(0,3)];

[s addAttribute:NSForegroundColorAttributeName

value:[NSColor greenColor]

range:NSMakeRange(0, 8)];

[s addAttribute:NSSuperscriptAttributeName

value:[NSNumber numberWithInt:-1]

range:NSMakeRange(9,5)];

Once you have an attributed string, you can do lots of stuff with it.

[s drawInRect:[self bounds]];

// Put it in a text field

[textField setAttributedStringValue:s];

// Put it on a button

[button setAttributedTitle:s];

Figure 20.2 shows the result of this code’s execution.

Figure 20.2. Using the Attributed String

Here are the names of the global variables for the most commonly used attributes, the type of object they correspond to, and their default values:

A list of all the attribute names can be found in NSAttributedString.h.

The easiest way to create attributed strings is from a file. NSAttributedString can read and write the following file formats:

• A string: Typically from a plain text file.

• RTF: Rich Text Format is a standard for text with multiple fonts and colors. In this case, you will read and set the contents of the attributed string

Return Main Page Previous Page Next Page

®Online Book Reader