NSAttributedString won't drawInRect:
NSAttributedString won't drawInRect:
- Subject: NSAttributedString won't drawInRect:
- From: Joel Lachter <email@hidden>
- Date: Fri, 23 May 2003 11:58:18 -0700
Hi,
I am a newbie so this may be something obvious, but I sure am at a
loss to figure it out. I want to create a view which changes at
regular intervals. I set up a timer which sends the view update
messages. Within the update message I have an
NSMutableAttributedString to which I send a drawInRect message. The
problem is that the AttributedString never gets drawn. If I
substitute an NSString, it does get drawn. And here is the really
weird part if I put in both the AttributedString and the String then
the AttributedString is drawn. The same code works the way I would
expect (i.e., the AttributedString draws by itself) if I put it in
the view's drawRect method.
I stripped out all the other code in this view and put it in a new
project. Still the same. Here is the code:
#import <AppKit/AppKit.h>
@interface StroopView : NSView {
NSTimer* update_timer;
}
- (void) update: (id) object;
@end
@implementation StroopView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
update_timer = [[NSTimer scheduledTimerWithTimeInterval: 1.0
target: self selector:@selector(update:)
userInfo:self repeats:true] retain];
}
return self;
}
- (void) dealloc{
[update_timer release];
[super dealloc];
}
- (void)drawRect:(NSRect)rect {
}
- (void) update: (id) object
{
NSString *s;
NSMutableAttributedString *as;
s = [NSString stringWithFormat:@"Test"];
as = [[NSMutableAttributedString alloc] initWithString:s];
[as addAttribute:NSFontAttributeName value:[NSFont
fontWithName:@"Courier" size:24] range:NSMakeRange(0, [s length])];
[as addAttribute:NSForegroundColorAttributeName value:[NSColor
greenColor] range:NSMakeRange(0, [s length])];
[self lockFocus];
// [s drawInRect:[self frame] withAttributes:nil];
[as drawInRect:[self frame]];
[self unlockFocus];
[as release];
}
@end
This view draws nothing. If I uncomment [s drawInRect:[self frame]
withAttributes:nil], it prints test out twice, (once in black once in
green). Any help appreciated.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.