NSView自定义字体的渲染不一致

时间:2017-07-10 11:26:25

标签: objective-c macos cocoa nsview nsbutton

我正在使用自定义字体以及我的按钮的属性标签,并且按钮似乎与打开NSWindow时的预期方式不同。

NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:centredStyle,
                       NSParagraphStyleAttributeName,
                       [NSFont fontWithName:@"Raleway-Bold" size:button_size],
                       NSFontAttributeName,
                       _red,
                       NSForegroundColorAttributeName,
                       nil];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"DELETE ALL" attributes:attrs];
[deleteNotifications setAttributedTitle:attributedString];

由于某种原因,我无法解决按钮的呈现方式(错误):

enter image description here

enter image description here

请注意,后者(正确)图像具有不透明度设置,第一个渲染似乎忽略了这一点,并且还注意到NSShadow的渲染方式也不同。

我无法识别按钮何时发生变化我知道它总是正确启动。分配按钮后,我将setWantsLayer设置为yes:

MyButton *deleteNotifications = [[MyButton alloc] initWithFrame:CGRectMake(window_width / 2 + (p /2), button_y, window_width /2 - p, 25)];
[deleteNotifications setWantsLayer:YES];
[deleteNotifications.layer setOpacity:min_opac];
[deleteNotifications setOpacity_min:min_opac];
[deleteNotifications setButtonType:NSMomentaryChangeButton];

当点击NSTextField对象时,我也遇到了类似的问题,其中字体变得比渲染更大胆。 (我相信按钮的解决方案也可以解决这个问题)

点击之前: enter image description here

点击(焦点)后: enter image description here

myButton的

@interface MyButton: NSButton
@property (strong) NSTrackingArea* trackingArea;
@property float opacity_min;
@end

@implementation MyButton
-(void)mouseEntered:(NSEvent *)theEvent {
    [super resetCursorRects];
    [self addCursorRect:self.bounds cursor:[NSCursor pointingHandCursor]];

    CABasicAnimation *flash = [CABasicAnimation animationWithKeyPath:@"opacity"];
    flash.fromValue = [NSNumber numberWithFloat:self.opacity_min];

    NSMutableArray *notifications = [[[NSUserDefaults standardUserDefaults] objectForKey:@"notifications"] mutableCopy];
    if((int)[notifications count] != 0){
        flash.toValue = [NSNumber numberWithFloat:1];
    }

    flash.duration = 0.2;
    [flash setFillMode:kCAFillModeForwards];
    [flash setRemovedOnCompletion:NO];
    flash.repeatCount = 1;
    [self.layer addAnimation:flash forKey:@"flashAnimation"];
}

-(void)mouseExited:(NSEvent *)theEvent{
    [super resetCursorRects];

    CABasicAnimation *flash = [CABasicAnimation animationWithKeyPath:@"opacity"];

    NSMutableArray *notifications = [[[NSUserDefaults standardUserDefaults] objectForKey:@"notifications"] mutableCopy];
    if((int)[notifications count] != 0){
        flash.fromValue = [NSNumber numberWithFloat:1];
    }

    flash.toValue = [NSNumber numberWithFloat:self.opacity_min];
    flash.duration = 0.2;
    [flash setFillMode:kCAFillModeForwards];
    [flash setRemovedOnCompletion:NO];
    flash.repeatCount = 1;
    [self.layer addAnimation:flash forKey:@"flashAnimation"];
}

-(void)updateTrackingAreas
{
    if(self.trackingArea != nil) {
        [self removeTrackingArea:self.trackingArea];
    }

    int opts = (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways);
    self.trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds]
                                                     options:opts
                                                       owner:self
                                                    userInfo:nil];

    [self addTrackingArea:self.trackingArea];
}
@end

的预感

我认为在显示窗口时可能与makeKeyAndOrderFront有关。

0 个答案:

没有答案