0

每个人!如果有人可以帮助我,我会很高兴^)

问题是:我的班级中有 IBAction 方法(单击复选框),我想通过单击复选框(变为粗体状态)来更改字体属性。我试图更改 IBAction 方法中的局部类变量,以便在我的-drawRect:方法中检查它,因为它不起作用 - 变量不会改变。如何在 IBAction 方法中更改局部变量,或者可能有另一种方法?谢谢。

#import <Foundation/Foundation.h>
@interface BigLetterView : NSView {
NSColor *bgColor;
NSString *string;
NSString *testString;
BOOL isHighlighted;
BOOL isBold;
BOOL isItalic;
IBOutlet NSButton *boldButton;
NSMutableDictionary *attributes;

}
@property(retain, readwrite) NSColor *bgColor;
@property(copy, readwrite) NSString *string;
@property(readwrite) BOOL isBold;

- (void)drawStringCenteredIn:(NSRect)r;
- (void)prepareAttributes;
- (void)changeIsBold;
- (IBAction)savePDF:(id)sender;
- (IBAction)makeBold:(id)sender; //it's that method
- (IBAction)setItalic:(id)sender;

下面的变量“isBold”变为“YES”。(方法被调用 - 我在调试器中测试)。

- (IBAction)makeBold:(id)sender
{
if ([boldButton state] == NSOnState) {
isBold = YES;
NSLog(@"Action bold=%d", isBold);
}
else {
isBold = NO;
NSLog(@"Action bold=%d", isBold);
}

}

但这里 isBold 仍然是“不”。

- (void)drawRect:(NSRect)rect {
NSRect bounds = [self bounds];
[bgColor set];
[NSBezierPath fillRect:bounds];
[self drawStringCenteredIn:bounds];
NSLog(@"isBold: %d",isBold); //HERE prints 'isBold:0'
if (isBold == YES) { 
NSFont *aFont = [attributes objectForKey:NSFontAttributeName];
NSFontManager *fontManager = [NSFontManager sharedFontManager];
[attributes setObject:[fontManager convertFont:aFont toHaveTrait:NSBoldFontMask] forKey:NSFontAttributeName];
}

//whether this view is firstResponder
if ([[self window] firstResponder] == self && 
[NSGraphicsContext currentContextDrawingToScreen]) {
[NSGraphicsContext saveGraphicsState];
NSSetFocusRingStyle(NSFocusRingOnly);
[NSBezierPath fillRect:bounds];
[NSGraphicsContext restoreGraphicsState];

}
} // drawRect

PSI 从 Aaron Hillegass 的书中做第 20 章。

4

1 回答 1

0

到目前为止,您所展示的内容看起来不错,但显然它不起作用,或者您不会在这里。尝试将此行添加到-makeBold:方法的末尾:

[self setNeedsDisplay];

这将迫使视图重新绘制自己;您的问题可能只是视图没有被重绘。

于 2010-12-14T18:59:03.993 回答