NSButton Toggle:OnClick 标题在 OSX 10.11 上被删除

NSButton Toggle : OnClick title get removed on OSX 10.11

我在 XIB 中使用切换按钮,

in AwakeFromNib :我设置了按钮的标题和背景图片。 代码在 OSX 10.10.5 及以下 之前运行良好,但在 更高版本 上,当我单击 按钮时文本被删除.

设置属性标题

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

任何想法都应该完成。

所以终于做对了

子类化 NSButtonCell 帮助了我

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

利用这个习俗Class

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

并解决了....