NSView incompatible rendering of custom fonts

I use a custom font along with an attributed label for my button, and every so often it seems that the button looks different than I expect when opened 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];

      

For some reason I cannot work, the button is displayed either as ( incorrect ):

enter image description here

or

enter image description here

Note that the last ( correct ) image has an opacity set that appears to ignore the first render and also notices that NSShadow

it renders differently as well.


I can't figure out when a button changes all I know is that it always fires up correctly. I have setWantsLayer

set yes after highlighting the button:

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];

      


I am also having a similar problem when clicking on an object NSTextField

where the font becomes bolder than it was rendered. (I believe the button solution will solve this as well)

Before clicking: enter image description here

After click (focus): 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

      


a guess

I believe it might have something to do with makeKeyAndOrderFront

when the window is shown.

+3


source to share





All Articles