Online Book Reader

Home Category

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

By Root 825 0
with an instance of NSData.

• RTFD: This is RTF with attachments. Besides the multiple fonts and colors of RTF, you can have images.

• HTML: The attributed string can do basic HTML layout, but you probably want to use the WebView for best quality.

• Word: The attributed string can read and write simple .doc files.

• OpenOffice

When you read a document in, you may want to know some things about it, such as the paper size. If you supply a place where the method can put a pointer to a dictionary, the dictionary will have all the extra information that it could get from the data. For example:

NSDictionary *myDict;

NSData *data = [NSData dataWithContentsOfFile:@"myfile.rtf"];

NSAttributedString *aString;

aString = [[NSAttributedString alloc] initWithRTF:data

documentAttributes:&myDict];

If you don’t care about the document attributes, just supply NULL.

Drawing Strings and Attributed Strings


Both NSString and NSAttributedString have methods that cause them to be drawn onto a view. NSAttributedString has the following methods:

- (void)drawAtPoint:(NSPoint)aPoint

Draws the receiver. aPoint is the lower-left corner of the string.

- (void)drawInRect:(NSRect)rect

Draws the receiver; all drawing occurs inside rect. If rect is too small for the string to fit, the drawing is clipped to fit inside rect.

- (NSSize)size

Returns the size that the receiver would be if drawn.

NSString has analogous methods. With NSString, you need to supply a dictionary of attributes to be applied for the entire string.

- (void)drawAtPoint:(NSPoint)aPoint

withAttributes:(NSDictionary *)attribs

Draws the receiver with the attributes in attribs.

- (void)drawInRect:(NSRect)aRect

withAttributes:(NSDictionary *)attribs

Draws the receiver with the attributes in attribs.

- (NSSize)sizeWithAttributes:(NSDictionary *)attribs

Returns the size that the receiver would be if drawn with the atttibutes in attribs.

Making Letters Appear


Open BigLetterView.h. Add an instance variable to hold the attributes dictionary and declare prepareAttributes.

#import

@interface BigLetterView : NSView {

NSColor *bgColor;

NSString *string;

NSMutableDictionary *attributes;

}

- (void)prepareAttributes;

Open BigLetterView.m. Create a method that creates the attributes dictionary with a font and a foreground color:

- (void)prepareAttributes

{

attributes = [NSMutableDictionary dictionary];

[attributes setObject:[NSFont userFontOfSize:75]

forKey:NSFontAttributeName];

[attributes setObject:[NSColor redColor]

forKey:NSForegroundColorAttributeName];

}

In the initWithFrame: method, call the new method:

- (id)initWithFrame:(NSRect)rect

{

self = [super initWithFrame:rect];

if (self) {

NSLog(@"initializing view");

[self prepareAttributes];

bgColor = [NSColor yellowColor];

string = @" ";

}

return self;

}

In the setString: method, tell the view that it needs to redisplay itself:

- (void)setString:(NSString *)c

{

string = c;

NSLog(@"The string: %@", string);

[self setNeedsDisplay:YES];

}

Create a method that will display the string in the middle of a rectangle:

- (void)drawStringCenteredIn:(NSRect)r

{

NSSize strSize = [string sizeWithAttributes:attributes];

NSPoint strOrigin;

strOrigin.x = r.origin.x + (r.size.width - strSize.width)/2;

strOrigin.y = r.origin.y + (r.size.height - strSize.height)/2;

[string drawAtPoint:strOrigin withAttributes:attributes];

}

Call that method from inside your drawRect: method:

- (void)drawRect:(NSRect)rect

{

NSRect bounds = [self bounds];

[bgColor set];

[NSBezierPath fillRect:bounds];

[self drawStringCenteredIn:bounds];

if (([[self window] firstResponder] == self) && ...

Build and run the application. Note that keyboard events go to your view unless they trigger a menu item. Try pressing Command-W. It should close the window (even if your view is the first responder for the key window).

Getting Your View to Generate PDF Data


All the drawing

Return Main Page Previous Page Next Page

®Online Book Reader