2016-12-30 1 views
0

J'utilise un bouton à bascule dans XIB,NSButton Bascule: titre Surclic supprimeront sur OSX 10,11

dans awakeFromNib: Je mets le titre et l'image BG du bouton. Le code fonctionne très bien jusqu'à OSX 10.10.5 et ci-dessous mais sur versions supérieures lorsque je clique sur le bouton le texte est retiré.

Set Intitulé attribué

[self.deleteButton setWantsLayer:YES]; 
    self.deleteButton.layer.backgroundColor = [self setButtonBGColor]; 
    [self setButtonTitleFor:self.deleteButton 
        toString:@"Delete"]; 
    [self.deleteButton setLayerContentsRedrawPolicy:NSViewLayerContentsRedrawOnSetNeedsDisplay]; 



- (void)setButtonTitleFor:(NSButton*)button toString:(NSString*)title 
{ 
    NSAttributedString *attrString = [self getAttributedStringForString:title]; 
    [button setAttributedTitle:attrString]; 
} 

enter image description here enter image description here

Tout chapeau idée doit être fait.

enter image description here

+0

Quelqu'un peut-il fournir de l'aide? – Swati

Répondre

0

donc finalement obtenu le droit

Subclassing NSButtonCell m'a aidé

RPButtonTextTopCell.h

@interface RPButtonTextTopCell: NSButtonCell

@end

RPButtonTextTopCell.m

@implementation RPButtonTextTopCell

-(id) init 
{ 
    self = [super init]; 
    if (self) { 
    } 
    return self; 
} 

-(id) initWithCoder:(NSCoder *)decoder 
{ 
    return [super initWithCoder:decoder]; 
} 

-(id) initTextCell:(NSString *)string 
{ 
    return [super initTextCell:string]; 
} 

-(NSRect)titleRectForBounds:(NSRect)theRect 
{ 
    NSRect titleFrame = [super titleRectForBounds:theRect]; 
    NSSize titleSize = [[self attributedStringValue] size]; 

    titleFrame.origin.y = (theRect.origin.y - (theRect.size.height-titleSize.height)*0.5) + 5; 

    return titleFrame; 
} 

@end

L'utilisation de cette classe personnalisée

RPButtonTextTopCell *deleteCell = [[RPButtonTextTopCell alloc] init]; 
deleteCell.backgroundColor  = [self setNSButtonBGColor]; 

[self.deleteButton setCell:deleteCell]; 
[self setButtonTitleFor:self.deleteButton 
       toString:@"Delete"]; 
[self.deleteButton setAction:@selector(deleteButtonClicked:)]; 
[self.deleteButton setTarget:self]; 

et son résolu ....